Issue
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?
Solution
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();
// ^^
Answered By - DSlomer64
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.