simplify MainActivity back press handling (#456)

This commit is contained in:
Konrad Pozniak 2017-11-13 20:43:16 +01:00 committed by Ivan Kupalov
parent ddc4954f8a
commit ed22d65c68

View File

@ -22,7 +22,7 @@ import android.content.res.Configuration;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.PersistableBundle; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout; import android.support.design.widget.TabLayout;
@ -60,7 +60,6 @@ import com.squareup.picasso.Picasso;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Stack;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
@ -84,7 +83,6 @@ public class MainActivity extends BaseActivity implements ActionButtonActivity {
private FloatingActionButton composeButton; private FloatingActionButton composeButton;
private String loggedInAccountId; private String loggedInAccountId;
private String loggedInAccountUsername; private String loggedInAccountUsername;
private Stack<Integer> pageHistory;
private AccountHeader headerResult; private AccountHeader headerResult;
private Drawer drawer; private Drawer drawer;
private ViewPager viewPager; private ViewPager viewPager;
@ -94,14 +92,6 @@ public class MainActivity extends BaseActivity implements ActionButtonActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
pageHistory = new Stack<>();
if (savedInstanceState != null) {
List<Integer> restoredHistory = savedInstanceState.getIntegerArrayList("pageHistory");
if (restoredHistory != null) {
pageHistory.addAll(restoredHistory);
}
}
FloatingActionButton floatingBtn = findViewById(R.id.floating_btn); FloatingActionButton floatingBtn = findViewById(R.id.floating_btn);
ImageButton drawerToggle = findViewById(R.id.drawer_toggle); ImageButton drawerToggle = findViewById(R.id.drawer_toggle);
TabLayout tabLayout = findViewById(R.id.tab_layout); TabLayout tabLayout = findViewById(R.id.tab_layout);
@ -165,15 +155,6 @@ public class MainActivity extends BaseActivity implements ActionButtonActivity {
public void onTabSelected(TabLayout.Tab tab) { public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition()); 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); tintTab(tab, true);
} }
@ -214,14 +195,6 @@ public class MainActivity extends BaseActivity implements ActionButtonActivity {
composeButton = floatingBtn; composeButton = floatingBtn;
} }
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
ArrayList<Integer> pageHistoryList = new ArrayList<>();
pageHistoryList.addAll(pageHistory);
outState.putIntegerArrayList("pageHistory", pageHistoryList);
super.onSaveInstanceState(outState, outPersistentState);
}
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
@ -253,11 +226,10 @@ public class MainActivity extends BaseActivity implements ActionButtonActivity {
public void onBackPressed() { public void onBackPressed() {
if (drawer != null && drawer.isDrawerOpen()) { if (drawer != null && drawer.isDrawerOpen()) {
drawer.closeDrawer(); drawer.closeDrawer();
} else if (pageHistory.size() < 2) { } else if (viewPager.getCurrentItem() != 0) {
super.onBackPressed(); viewPager.setCurrentItem(0);
} else { } else {
pageHistory.pop(); super.onBackPressed();
viewPager.setCurrentItem(pageHistory.peek());
} }
} }
@ -442,7 +414,7 @@ public class MainActivity extends BaseActivity implements ActionButtonActivity {
mastodonApi.accountVerifyCredentials().enqueue(new Callback<Account>() { mastodonApi.accountVerifyCredentials().enqueue(new Callback<Account>() {
@Override @Override
public void onResponse(Call<Account> call, Response<Account> response) { public void onResponse(@NonNull Call<Account> call, @NonNull Response<Account> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
onFetchUserInfoSuccess(response.body(), domain); onFetchUserInfoSuccess(response.body(), domain);
} else { } else {
@ -451,7 +423,7 @@ public class MainActivity extends BaseActivity implements ActionButtonActivity {
} }
@Override @Override
public void onFailure(Call<Account> call, Throwable t) { public void onFailure(@NonNull Call<Account> call, @NonNull Throwable t) {
onFetchUserInfoFailure((Exception) t); onFetchUserInfoFailure((Exception) t);
} }
}); });