Monday, 30 September 2013

How to make a fragment be in the exact same way I left it when I return to it from another fragment?

How to make a fragment be in the exact same way I left it when I return to
it from another fragment?

I have a few fragments in my app, but my code opens a new fragment every
time I click the button.
I want to know how can I change this, and make the fragment return to the
exact same state I left it in.
The code im using right now:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragments);
MainActivity fragment = new MainActivity();
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.add(R.id.fragment_place, fragment);
transaction.commit();
turnGPSOn();
}
public void onSelectFragment(View view) {
if (view == findViewById(R.id.add))
{
newFragment = new Add();
}
else if (view == findViewById(R.id.map))
{
newFragment = new MainActivity();
}
else
{
newFragment = new Add();
}
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_place, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
Thanks!

No comments:

Post a Comment