8

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.

fuadj
  • 434
  • 4
  • 10

2 Answers2

8

You can set the data on the activity's intent before calling recreate

        getIntent().putExtra("RECREATE_DATA", "Some Data");
        recreate()

since when you recreate the activity the same activity instance is used, the data in the intent will still be there after recreate.

Ismail Shaikh
  • 482
  • 4
  • 13
3

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

Raman
  • 166
  • 1
  • 6
  • But I want oncreate to be called, the whole point of the question was to handle preference changes(that change the whole UI considerably), I do UI stuff in oncreate, so I need it to be called – fuadj Aug 04 '16 at 06:44
  • one way is to create all the views in onCreate and setvisibility false to the set of UI needed after restart and once you get on onNewIntent, use the intent and set the visibility of these set of view true and the other set false. This way user will not be aware that the activity was relaunched. – Raman Aug 04 '16 at 06:54