I have five activity for example
A, B, C, D, E
I am going from A -> B -> C -> D -> E
now when i press button from E
i want to go to directly B and B should not create new actiivity
I know that
Intent intent = new Intent(this, ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
by this i can do it but i don't want to create activity B by doing startActivity
In the button's onClick you can have the following:
Intent intent = new Intent(this, ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
In your application manifest file define the Activity like
<activity
android:name="com.example.taskandbackstackexample.BActivity"
android:label="#string/title_activity_b"
android:launchMode="singleTask" >
</activity>
and start your activity as
Intent i = new Intent(this, BActivity.class);
startActivity(i);
The activity's "launchMode" attribute has value "singleTask". The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance. Only one instance of the activity can exist at a time.
Related
I have android activity flow somewhat like
A->B->A
Moving from A to B and from B to A using intent and finish Activity B. On when I click on backpress I again go to activity A instead of exit from application.
I tried to set Intent Flags but was not able to achieve it.
Here is code to move from Activity A -> Activity B
Intent intent = new Intent(A.this, B.class);
startActivity(intent);
If here I backpress than I need to go to activity A again
From Activity B -> Activity A
Intent intent = new Intent(B.this, A.class);
startActivity(intent);
finish();
But when I backpress from here I need to exit the application
To go from B to A you don't need to do anything. If the user presses the BACK key, the default implementation calls finish() on B which will return the user to A.
If you have provided a button or something in B to go back to A, just call finish().
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 have 3 Activities:
A: Parent Activity
B: first Child Activity
C: second Child Activity
In A Activity open B Activity when button is clicked
Intent i = new Intent(this, ActivityB.class);
startActivityForResult(i, 1);
In A Activity there is a Runnable object that in some condition open C Activity
Intent i = new Intent(this, Activityc.class);
startActivityForResult(i, 2);
All work and on device i see C Activity over B Activity over A Activity but when i close C Activity with this.finish(); in A Activity onActivityResult() event is not fired. It'll be fired only if i close the B Activity too.
How can i know in A Activity when C Activity is closed and let B Activity opened?
Whenever you wish to exit all open activities, you should press a button which loads the first Activity that runs when your application starts then clear all the other activities, then have the last remaining activity finish. to do so apply the following code in ur project
Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
The above code finishes all the activities except for FirstActivity. Then we need to finish the FirstActivity's Enter the below code in Firstactivity's oncreate
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
Please See this
I have three activities (a, b, c).
Activity a
- has one button for activity b and one button to close the app (by calling onfinish()).
- is the main activity, which runs b.
Activity b
- is the middle one which runs c.
After clicking on a back button that I placed in the header of activity c, and then clicking on the button of the a activity to close the app, I am returned to activity c. This is not the behavior I expected, any ideas about what is happening?
Further information:
activity c has onResume() because I also have an activity d. so when I click back button of activity d, it returns to activity c.
so long as I don't run the activity c, the close button works as expected.
try this.
Intent intent = new Intent(getApplicationContext() or UrActivity.this, FirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
For going previous activity just call
finish() ;
at back button of any activity .don't do this
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
and from exiting the application use
Intent intent = new Intent(getApplicationContext(), a.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
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