Search code examples
androidandroid-activityrecreate

Recreate an activity and also pass arguments


I have an activity that listens to preference changes and reloads the app. I am using recreate() to do that. But I don't know how to pass in arguments through that, so I have resorted to manual activity reloading.

Intent intent = getIntent();
finish();
// add in the arguments as Extras to the intent
startActivity(intent);

This has the behaviour I want, but the recreating of the activity isn't smooth for the user as they will see the activity being killed and the same activity relaunching. I want the user to not be aware that the activity was relaunched. So, my question is can I use the method recreate() and still pass arguments through it.


Solution

  • You can try this way: You can restart you activity with launch Mode as SingleTop and handle the onNewIntent(Intent intent) method. This way you are restarting the activity and send the intent, along with this activity is not being Killed i.e. oncreate of your activity will not be called.

    public class MainActivity extends Activity implements View.OnClickListener {
        Button btn ;
        String mRelaunchData ;
        public static String TAG = "RelaunchMainActivity";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btn = (Button)findViewById(R.id.button);
            btn.setOnClickListener(this);
            Log.e(TAG, "onCreate called");
        }
    
        @Override
        public void onClick(View view) {
            Log.e(TAG, "onClick called");
            Intent intent = new Intent("relaunch.activity.ACTIVITY_SELF_START_INTENT").setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            intent.putExtra("RESTART_DATA", "This is relaunch of this Activity");
            startActivity(intent);
        }
    
        @Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);
            Log.e(TAG, "onNewIntent called");
            mRelaunchData = intent.getStringExtra("RESTART_DATA");
            Log.e(TAG, "mRelaunchData =" + mRelaunchData);
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            Log.e(TAG, "onResume called");
            if(mRelaunchData != null){
                Toast.makeText(MainActivity.this, mRelaunchData, Toast.LENGTH_SHORT).show();
            }
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            Log.e(TAG, "onPause called");
    
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            Log.e(TAG, "onStart called");
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            Log.e(TAG, "onStop called");
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            Log.e(TAG, "onDestroy called");
        }
    }
    

    in AndroidManifest.xml

     <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <action android:name="relaunch.activity.ACTIVITY_SELF_START_INTENT" />
                    <category android:name = "android.intent.category.DEFAULT"/>
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    

    onClick will relaunch the Activity.

    LifeCycle will be

    -onclick

    -onPause

    -onNewIntent

    -onResume