I got two activities, one active and other paused and I want to close/finish all the two activities and start another intent. I tried this inside the active activity:
finish();
Intent intent = new Intent(getApplicationContext(), Init.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
then I tried to close the program inside Init class and it appeared me the previous paused activity. How can I solve this problem ?
Try calling not calling finish() before you launch the newest intent and use these flags
Intent intent = new Intent(getApplicationContext(), Init.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Related
I'm trying to open an already activity with animation open.
It will run it only first time which activity starts and then it will not work again
Here is the code :
var intent = new Intent(this, typeof(TableItemsMain))
.SetFlags(ActivityFlags.ReorderToFront);
StartActivity(intent);
OverridePendingTransition(Resource.Layout.trans_left_in, Resource.Layout.trans_left_out);
So i got two activities, a main then a secondary, we'll call them activity_A (main) and activity B( the second ).
So my activity_A is getting some data, then sending it to activity_B with this code :
Intent i = new Intent(this,activity_B.class);
i.putExtra("Charge_Batterie", bat);
startActivity(i);
activity_B receive it correctly with this code :
String bat = getIntent().getStringExtra("Charge_Batterie");
My problem is that my activity_A is refreashing it's value "bat", so when i send it with intent it doesn't refreash in activity_B.
So i'm wondering, is my activity_A sleeping ? If yes how can i make it stay alive ?
I´m doing the same thing and it works for a long time - activity A runs a timer and launches activity B with extras.
When I want to refresh activity B with "new extras", I´m doing this:
// check if activity B is created and running:
if (intent == null) {
System.out.println("Creating Activity - Intent is null");
intent = new Intent(context, ActivityB.class);
intent.putExtras(ImagedownloaderActivityBundle);
startActivity(intent);
}else {
System.out.println("Reloading Activity - Intent exists");
finish();
intent = new Intent(context, ActivityB.class);
intent.removeExtra("playlistsList");
intent.putExtras(ImagedownloaderActivityBundle);
startActivity(intent);
}
As far as I know, this isn´t the best way to achieve the background task - it should be running on a service, perhaps doing the same when one needs to refresh.
I want to restart my Android app, but I want it to go to a specific activity after the app restart.
I am currently using this code to re launch my app, but it starts back to the first activity. I need it to go to another specific activity.
I made a settings page in my application where users can choose their own app colors. After choosing a color, i need to restart all activities to apply the new theme that the user chose.
code I am using to restart app to first activity:
Intent startActivity = new Intent();
startActivity.setClass(ProfileSettingsActivity.this,ProfileSettingsActivity.class);
startActivity(startActivity);
finish();
easy
Intent intent = new Intent(this, ANOTHER_SPECIFIC_ACTIVITY.class);
this.startActivity(intent);
finish();
In the onCreate() of your first activity, check for whatever conditions you want, and then based on those conditions launch the intent for your specific activity before the setContentView() is called in the first activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(CONDITION_1) {
startActivity(new Intent(this, ActivityA.class));
} else if(CONDITION_2) {
startActivity(new Intent(this, ActivityB.class));
}
setContentView(R.layout.activity_first);
}
Have you tried clearing all activities and launch the new one with Flags?
Intent intent = new Intent(this, NEW_ACTIVITY.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Doing this, you are clearing all activities previously created and launch NEW_ACTIVITY.class with the new configuration.
This question already has answers here:
Android: Clear the back stack
(39 answers)
Android: Remove all the previous activities from the back stack
(18 answers)
Closed 9 years ago.
For example,
I have activity A, B, C, D
A call B
Intent intent = new Intent(A,B.class);
startActivity(intent);
Then, B call C
Intent intent = new Intent(B,C.class);
startActivity(intent);
After that, C call D
Intent intent = new Intent(C,D.class);
startActivity(intent);
In Activity D, I call finish(). It will return back to Activity C.
My question is how can I clear Activity A, B, C before calling finish() so that the app quit like normal.
Don't suggest call finish() on every startactivity because the app can press back to previous activity to continue.
This should work definitely...
Intent intent = new Intent(D,A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("close",true);
startActivity(intent);
and in oncreat of A activity u have to write
if (getIntent().getBooleanExtra("close", false)) {finish();
}
else {
{
//ur previous code here
}
Have fun if any problem u can ask
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
FLAG_ACTIVITY_CLEAR_TASK
FLAG_ACTIVITY_NEW_TASK
which ensures that if an instance is already running and is not top then anything on top of it will be cleared and it will be used, instead of starting a new instance (this useful once you've gone Activity A -> Activity B and then you want to get back to A from B, but the extra flags shouldn't affect your case above).
Try adding FLAG_ACTIVITY_NEW_TASK.
So your code would be:
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I am using the following in my application. Hope it will help.
Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // this will clear the stacks
intent.putExtra("exitme", true); // tell Activity A to exit right away
startActivity(intent);
and in Activity A add the following:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
if( getIntent().getBooleanExtra("exitme", false)){
finish();
return;
}
}
try with Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
see here
http://developer.android.com/reference/android/content/Intent.html
I have an app that I'm writing that starts with an initial Login/Create New Account splash screen. In onCreate it checks my SharedPreferences to see if user credentials are stored. If they are the activity launches an intent to the main activity skipping the login/create process.
if (haveCredentials()) {
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
I don't want the user to be able to go back to the splash screen. As you can see I'm trying the CLEAR_TOP Intent flag as suggested by this but it doesn't seem to be working. Not sure what I'm missing.
Current State of the Activity Stack
SplashActivity->MainActivity
Desired State of the Stack
MainActivity
Have you tried finishing the current activity?
if (haveCredentials()) {
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish(); //add this!
}
Just use finish() after startActivity(), so that when back button is pressed the previous activity will not be shown.