Reputation: 4283
I'm trying to learn the Fragment
class. Here's MainActivity
:
package com.dslomer64.hour8appflip;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button btnStartGarcon, btnStartWaiter;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStartGarcon = (Button)findViewById(R.id.btnStartGarcon);
btnStartWaiter = (Button)findViewById(R.id.btnStartWaiter);
ableButtons(true);
}
@Override protected void onStart(){
super.onStart();
ableButtons(true);
}
@Override protected void onRestart(){
super.onRestart();
ableButtons(true);
}
@Override protected void onResume(){
super.onResume();
ableButtons(true);
}
public void showFragmentGarcon(){
FragmentGarcon fragmentGarcon = new FragmentGarcon();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.layout_container, fragmentGarcon);
ft.addToBackStack("Garcon");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
public void showFragmentWaiter(){
FragmentWaiter fragmentWaiter = new FragmentWaiter();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.layout_container, fragmentWaiter);
ft.addToBackStack("Waiter");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
public void onStartGarcon(View view) {
showFragmentGarcon();
ableButtons(false);
}
public void onStartWaiter(View view) {
showFragmentWaiter();
ableButtons(false);
}
private void ableButtons(boolean b){
btnStartWaiter.setEnabled(b);
btnStartGarcon.setEnabled(b);
}
}
Upon launch, the screen is at left. After first button tap, it's at right, because I decided it would be best to disable the MainActivity buttons then.
But when I tap the back
icon enough times to exhause the backStack
, no matter what I do (see the very redundant ableButtons(true)
in onStart
, onRestart
onResume
), the buttons are still disabled.
What to do?
Upvotes: 0
Views: 41
Reputation: 4283
Figured it out (it has nothing to do with the Android life cycle):
FragmentManager fm = getFragmentManager();
...
@Override
public void onBackPressed() {
super.onBackPressed();
if( fm.getBackStackEntryCount() == 0){
ableButtons(true);
}
}
And use fm
in showFragmentGarcon
and the other showFrag...
:
public void showFragmentGarcon(){
FragmentGarcon fragmentGarcon = new FragmentGarcon();
FragmentTransaction ft = fm.beginTransaction();
// ^^
Upvotes: 0
Reputation: 157457
What part of the Android life cycle is involved (if any) when going back through backStack returns to main activity from a sequence of fragments?
The Activity life cycle is not involved. When you replace a Fragment you don't leave the activity, so you don't return to it.
But when I tap the back icon enough times to exhause the backStack, no matter what I do (see the very redundant ableButtons(true) in onStart, onRestart onResume), the buttons are still disabled.
This is the expected behavior. You disabled the buttons and it is up to you enable them again when the backStack
is empty. For instance you could override onBackPressed
@Override
public void onBackPressed(){
int count = getSupportFragmentManager().getBackStackEntryCount();
if (count == 0){
ableButtons(true);
}
super.onBackPressed();
}
Upvotes: 1