From ed22d65c686fc74599851af5734ebf00465a5d64 Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Mon, 13 Nov 2017 20:43:16 +0100 Subject: [PATCH] simplify MainActivity back press handling (#456) --- .../com/keylesspalace/tusky/MainActivity.java | 40 +++---------------- 1 file changed, 6 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java index ebb311ed..9f9f82b3 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java @@ -22,7 +22,7 @@ import android.content.res.Configuration; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; -import android.os.PersistableBundle; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.TabLayout; @@ -60,7 +60,6 @@ import com.squareup.picasso.Picasso; import java.util.ArrayList; import java.util.List; -import java.util.Stack; import retrofit2.Call; import retrofit2.Callback; @@ -84,7 +83,6 @@ public class MainActivity extends BaseActivity implements ActionButtonActivity { private FloatingActionButton composeButton; private String loggedInAccountId; private String loggedInAccountUsername; - private Stack pageHistory; private AccountHeader headerResult; private Drawer drawer; private ViewPager viewPager; @@ -94,14 +92,6 @@ public class MainActivity extends BaseActivity implements ActionButtonActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - pageHistory = new Stack<>(); - if (savedInstanceState != null) { - List restoredHistory = savedInstanceState.getIntegerArrayList("pageHistory"); - if (restoredHistory != null) { - pageHistory.addAll(restoredHistory); - } - } - FloatingActionButton floatingBtn = findViewById(R.id.floating_btn); ImageButton drawerToggle = findViewById(R.id.drawer_toggle); TabLayout tabLayout = findViewById(R.id.tab_layout); @@ -165,15 +155,6 @@ public class MainActivity extends BaseActivity implements ActionButtonActivity { public void onTabSelected(TabLayout.Tab tab) { viewPager.setCurrentItem(tab.getPosition()); - if (pageHistory.isEmpty()) { - pageHistory.push(0); - } - - if (pageHistory.contains(tab.getPosition())) { - pageHistory.remove(pageHistory.indexOf(tab.getPosition())); - } - - pageHistory.push(tab.getPosition()); tintTab(tab, true); } @@ -214,14 +195,6 @@ public class MainActivity extends BaseActivity implements ActionButtonActivity { composeButton = floatingBtn; } - @Override - public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { - ArrayList pageHistoryList = new ArrayList<>(); - pageHistoryList.addAll(pageHistory); - outState.putIntegerArrayList("pageHistory", pageHistoryList); - super.onSaveInstanceState(outState, outPersistentState); - } - @Override protected void onResume() { super.onResume(); @@ -253,11 +226,10 @@ public class MainActivity extends BaseActivity implements ActionButtonActivity { public void onBackPressed() { if (drawer != null && drawer.isDrawerOpen()) { drawer.closeDrawer(); - } else if (pageHistory.size() < 2) { - super.onBackPressed(); + } else if (viewPager.getCurrentItem() != 0) { + viewPager.setCurrentItem(0); } else { - pageHistory.pop(); - viewPager.setCurrentItem(pageHistory.peek()); + super.onBackPressed(); } } @@ -442,7 +414,7 @@ public class MainActivity extends BaseActivity implements ActionButtonActivity { mastodonApi.accountVerifyCredentials().enqueue(new Callback() { @Override - public void onResponse(Call call, Response response) { + public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { onFetchUserInfoSuccess(response.body(), domain); } else { @@ -451,7 +423,7 @@ public class MainActivity extends BaseActivity implements ActionButtonActivity { } @Override - public void onFailure(Call call, Throwable t) { + public void onFailure(@NonNull Call call, @NonNull Throwable t) { onFetchUserInfoFailure((Exception) t); } });