droidShot
droidShot

Reputation: 137

Similar onBackPressed() for Fragments?

Hello my Android application is using fragments. I am wondering if there is a way I can use the idea of the onBackPressed() method in my app with fragments. I have previous and next buttons, but as of now I am just creating new fragments and replacing, and none of the data gets saved. Is there a way to save my data/go back once I have gone forward?

Upvotes: 0

Views: 287

Answers (3)

Anderson K
Anderson K

Reputation: 5505

Sorry, do not know if I understand your question, but if the idea and have control of direct backbutton in its fragment, and from it to perform some task of data persistence, you can add your fragment to control stack FragmentManager, the as follows.

FragmentManager fm = getSupportFragmentManager();

MyFragment mMyFragment = new MyFragment();

fm.beginTransaction()
    .add(mMyFragment, "mMyFragment")
    .addToBackStack( null )
    .commit();

In the fragment you need to implement the interface OnBackStackChangedListener

In Fragment:

public class MyFragment extends Fragment implements OnBackStackChangedListener {

    @Override
    public void onBackStackChanged() {
       //your code here
    }
}

If you just keep the values

public class MyFragment extends Fragment {

    String valeu;

    @Override
    public void onCreate( final Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );

        if ( savedInstanceState != null ) {
            this.valeu = savedInstanceState.getString( "key" );
        }
    }

    @Override
    public void onSaveInstanceState( final Bundle outState ) {
        super.onSaveInstanceState( outState );

        outState.putString( "key", "Your content" );
    }
}

Upvotes: 1

Marcelo Idemax
Marcelo Idemax

Reputation: 2810

The concept of Fragment is different of Activity.

One Activity could have a many Fragments, read that:

A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).

See more here: http://developer.android.com/guide/components/fragments.html


SOLUCTION

So if you wanna handle the onBackPressed behavior in you Fragment you could do that:

package com.example.stackoverflowsandbox;

import android.app.Activity;
import android.app.Fragment;

public class MainActivity extends Activity {

    public class MyFragment extends Fragment {
        public void onBackPressed() {
            // your code here...
        }
    }

    private MyFragment myFragment;

    @Override
    public void onBackPressed() {
        // super.onBackPressed(); // comment to not back

        this.myFragment.onBackPressed(); // the onBackPressed method of Fragment is a custom method
    }
}

Upvotes: 1

Shaz
Shaz

Reputation: 302

while moving to front fragment, save the state of previous fragment using onSaveInstanceState() while moving back restore the state in onCreate() or onCreateView() in the previous fragment

Upvotes: 0

Related Questions