简体   繁体   中英

How to navigate back to an activity with restarting it?

I have MainActivity and SubActivity, MainActivity starts SubActivity with code:

    startActivity(new Intent(getApplicationContext(), SubActivity.class));
    overridePendingTransition(R.anim.slide_up, R.anim.no_animation);

Than I do something in SubActivity and after that I have to return to MainActivity using custom animation. Also I need a way to update MainActivity when I return to it. This is the code from SubActivity to navigate back to MainActivity, which does not work at all:

    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(intent);
    overridePendingTransition(R.anim.no_animation, R.anim.slide_down);
    finish();

If I call:

    finish();
    overridePendingTransition(R.anim.no_animation, R.anim.slide_down);

The animation is applied and I am returned to unchanged MainActivity who started SubActivity.

If I call:

    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(intent);
    overridePendingTransition(R.anim.no_animation, R.anim.slide_down);

The MainActivity is recreated but I don't have my animation, also I don't know what happened to previous MainActivity which had started SubActivity.

The finish() method is described with:

 *Call this when your activity is done and should be closed.  The
 * ActivityResult is propagated back to whoever launched you via
 * onActivityResult().

But I don't know how to use this ActivityResult to update my MainActivity.

How can I return to MainActivity, using animation (which only works with finish()), and update it when I return to it?

Correct, finish() is the correct method to use to return to the previous activity, as startActivity(intent); is creating a new instance of that activity. To pass data to the previous this instance you can use SharedPreferences:

To store data before calling finish()

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score_key), newHighScore);
editor.commit();

To read from it when MainActivity onResume is called

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int highScore = sharedPref.getInt(getString(R.string.saved_high_score_key), 0);

https://developer.android.com/training/data-storage/shared-preferences

start sub activity using:

    Intent intent = new Intent(getApplicationContext(), SubActivity.class);
    startActivityForResult(intent, REQUEST_CODE);
    overridePendingTransition(R.anim.slide_up, R.anim.no_animation);

when you want to return to main activity:

    Intent intent = new Intent();
    intent.putExtra("extra_data", "from subactivity");
    setResult(Activity.RESULT_OK, intent);
    finish();
    overridePendingTransition(R.anim.no_animation, R.anim.slide_down);

Inside mainactivity use this method to get data sent by subactivity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == REQUEST_CODE) {

        if (resultCode == Activity.RESULT_OK) {
            String result = data.getStringExtra("extra_data");
            // do something with the result

        } else if (resultCode == Activity.RESULT_CANCELED) {
            // some stuff that will happen if there's no result
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM