Programmatically Relaunch/Recreate an Activity

Programmatically relaunch/recreate an activity?

UPDATE: Android SDK 11 added a recreate() method to activities.


I've done that by simply reusing the intent that started the activity. Define an intent starterIntent in your class and assign it in onCreate() using starterIntent = getIntent();. Then when you want to restart the activity, call finish(); startActivity(starterIntent);

It isn't a very elegant solution, but it's a simple way to restart your activity and force it to reload everything.

Recreate an activity and also pass arguments

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

Restart Activity onPostExecute

You can Simply use

startActivity(getIntent());
finish();

to refresh an Activity from within itself.

Alternatively you can call recreate() to refresh the activity.

recreate /restart activity from another activity

I don't know what exactly you mean with "activity A must stay opened" though so you'll have to figure out that part or give a more detailed explanation. Do you mean Activity B overlays Activity A? I'll assume that.

I suggest you to use 3 fragments in Activity A.
One fragment for the initial layout and another one for the new layout.
Then another fragment instead of Activity B, I'll call it FragmentB.

In FragmentB create a Callback inner interface.

public interface Callbacks {
void onLoggedIn();
}

Then make Activity A implement this callback

public class ActivityA extends Activity implements FragmentB.Callbacks {

//rest of the activity...

@Override
public void onLoggedIn() {

getFragmentManager().beginTransaction()
.replace(R.id.container, new SecondFragment())
.commit();
}
}

Obviously you can choose any name you like for ActivityA, FragmentB, Callbacks, onLOggedIn and SecondFragment.
Where SecondFragment is the name of the fragment that contains the layout after being logged in in FragmentB.

Reload activity in Android

You can Simply use

finish();
startActivity(getIntent());

to refresh an Activity from within itself.

Theme Programmatically set. How to reload Activity to apply

It is in the incorrect order.

    finish();
intent = new Intent(this, <your_activity>.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

This is the correct order.

Theme can be set before super.onCreate(savedInstanceState); is being called. You need to destroy the activity and create it again and call immediately setTheme(THEME); in onCreate()

Android - Relaunching an activity without having to re-create it

Set the activity launchmode to singletop or set the flag FLAG_ACTIVITY_SINGLE_TOP .

Handle the intent in onNewIntent()
You can get further details here

Edit: Though if back key is pressed on the activity or configuration changed since last time you entered the app, the activity will still be recreated



Related Topics



Leave a reply



Submit