For an App I am developing, I want to re-launch the current activity using an intent. So I'm in MainActivity.class and I want to re-launch MainActivity.class using the following:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
This calls onDestroy() but does not re-launch the activity. Why doesn't this work?
If your are in an Activity: this.recreate();
If your are in a Fragment: getActivity.recreate();
Related Links :
How do I restart an Android Activity
how do I restart an activity in android?
Android activity restart
You could just use:
finish();
startActivity(getIntent());
Which will finish the current activity, and start a new activity with the same intent that you received when the activity was originally created. This should effectively re-launch the activity as is.
Edit:
See Reload activity in Android
Just do this:
Intent i=getIntent();//This simply returns the intent in which the current Activity is started
finish();//This would simply stop the current Activity.
startActivity(i);//This would start a new Activity.
Include this one ...
startActivity(intent);
Related
I have a activity which i declare as launchMode="singleInstance in Manifest file. and using blow code to starting this activity:
Intent intent = new Intent(this, LockScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
To close this activity i am doing like: finishAffinity();
But some how when i finish this activity i can see another open activity instance of this. Can someone tell me how i can finish all OR only create a single one when i need to start this activity from different places inside my application?
I tried to set intent.setFlags (FLAG_ACTIVITY_CLEAR_TASK | FLAG_ACTIVITY_NEW_TASK), but the effect is not as I think.
It will execute onResume () in the old A activity.
But I want the effect as shown below:
A activity onCreate() ... onPause()
anywhere(from notification, from B activity ...) start A activity again
old A activity onDestory()
create new A activity
you should use FLAG_ACTIVITY_CLEAR_TOP
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
SAMPLE CODE
Intent i = new Intent(YourActivity.this, OTHERACTIVITY.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
use (FLAG_ACTIVITY_CLEAR_TOP) flag when u passing intent. then after startactivity() use finish() method.
I'm working on an android application and in the application I have a couple buttons that let user to pass to another activity. Now the way I'm doing the transitions between this Intents is like below:
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
With the above content I start an activity and from that activity I'm getting back to the MainActivity using this code:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
But i suspect this cause memory to be over used because I'm not actually getting back to the Main Activity that created when application started, I'm just creating another instance of MainActivity I guess. Is this really as i thought and if it is how can I get back to the activity that created in the beginning or if I can't do such thing how can I make app to let the previous activity go?
Passing an intent to startActivity() will create a new instance of the activity and add it to the front of the stack. So:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
is basically asking to create a new instance. If you want to go back to the activity just before the current one, call either:
finish();
Or,
super.onBackPressed();
In your solution you just have to press back button and you'll be back in first activity.
If you want to close it and after open new instance like you are doing in second activity just add
finish();
at the end of
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
You just need to call finish(); method
Intent intent = new Intent(this, DestinationActivity.class);
startActivity(intent);
finish();
I have a spinner that essentially starts multiple new activities based on onItemSelect();
However, what's happening on my app is if I start the same activity multiple times I have to hit the Android back button multiple times. How do I start the same activity and kill the previous one so that I don't have multiple layouts sitting there open?
Set android:noHistory=true for your <activity> in Manifest.xml. See here
Or, programatically:
Intent intent = new Intent(this, Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Use the FLAG_ACTIVITY_CLEAR_TOP activity flag.
When you start a new activity you need to finish your old activity. So when you call your new intent:
startActivity(intent);
finish();
That will end the current activity.
You might want to consider setting the activity launchmode to "singletop" (this can be done in the Android Manifest). Then instead of creating a new activity, it will call OnNewIntent() of the existing activity.
I can start activity from service but I cannot restart it, this flag only brings to front the existing activity:
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
how could I restart (finish the old, start new)?
It's bit a trickery but what you can do is to add something like RESTART=true to the intent that calls the Activity. In activity's onResume you can do getIntent and see if that flag exists and if so call finish() and call activity anew
Try finishing the activity first by calling:
activity.finish();
then do a new intent to start the activity again:
myIntent = new Intent(context, activity.class);
startActivity(myIntent);