Vote count:
0
I am building a calculator app using fragment and I want the calculator fragment to show back when the back button is pressed on the menu item fragment. I have been using the addToBackStac(null) but it is not working for me.
Here is my code below
public class MainActivity extends ActionBarActivity {
FragmentManager fragmentManager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null){
fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction().addToBackStack(null);
CalculatorFragment calc = new CalculatorFragment();
fragmentTransaction.add(R.id.relLejaut, calc);
fragmentTransaction.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.item) {
About about= new About();
// Insert the fragment by replacing any existing fragment
fragmentManager.beginTransaction().addToBackStack(null).replace(R.id.relLejaut, about).commit();
return true;
}
return super.onOptionsItemSelected(item);
}
}
asked 53 secs ago
Android using fragments add to back stack wont work
Aucun commentaire:
Enregistrer un commentaire