Android finishAndRemoveTask dosenot start second activity
this is the first activity
Intent i = new Intent(currentAct.this, secondAct.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("abcd", j);
startActivity(i);
finishAndRemoveTask();
If I dont add finishAndRemoveTask(); it works fine the secondAct starts
this is the manifest
<activity
android:name="currentAct"
android:noHistory="true"
android:theme="#style/Theme.AppCompat.Translucent"
android:taskAffinity=".ShortCutActivity">
</activity>
<activity
android:name="secondAct"
android:noHistory="true"
android:theme="#style/Theme.AppCompat.Light.NoActionBar.FullScreen"
android:taskAffinity=".ShortCutActivity">
ShortCutActivity is just a mainactivity
how can we start second activity and remove the first activity from stack also
use this before calling startActivity(intent)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
You should add flag FLAG_ACTIVITY_CLEAR_TASK for launching intent.
Intent intent = new Intent(this, Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Referring to docs:
If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.
Related
Now I have HomeActivity -> Activity A -> Activity B -> Activity C
After I finish Activity C I expect to see HomeActivity. How can I handle this flow with Intent Flags or any other suggestions?
edit
But Activity A and Activity B should be removed from the stack when Activity C appear.
Activity A B and C this a workflow, So A can go to B and B can go back to A to edit something, After I Submit in B then A and B should be gone and show C as a confirmation
Using launch modes you can achieve this.
use Single Task for the Activity C in manifest file
<activity android:name=".ActivityC"
android:launchMode="singleTask">
Use this code
Intent intent = new Intent(ActivityC.this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish(); // call this to finish the current activity
You should use
setFlags(...)
finish();
If set in an Intent passed to Context.startActivity(), this flag will
cause any existing task that would be associated with the activity to
be cleared before the activity is started. That is, the activity
becomes the new root of an otherwise empty task, and any old
activities are finished. This can only be used in conjunction with
FLAG_ACTIVITY_NEW_TASK.
Code Structure
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
You should kill the activity before starting the next one.
Intent intent = new Intent(ActivityC.this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
I have two activities in my app. There is a ListView in the first one and the second one is meant to represent the details for each item of the ListView. The problem is that when I call the second Activity it always opens the same thing but not a new one for each item. Any suggestions?
This is how I changed the calling of the second activity but it is not really working for me.
if (item.getTitle().equals("Open")){
Intent intent = new Intent();
intent.setClass(this, DetailsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
You need to make that activity singleTop and pass the flag clear top when you are starting it.
in AndroidManifest.xml:
<activity
android:name=".SomeActivity"
android:label="SomeLabel"
android:launchMode="singleTop">
</activity>
in your calling Activity:
Intent i = new Intent();
i.setClass(this, SomeActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
You could also be finishing the second activity by calling finish(); when the user leaves it.
I am developing small android application in which I have 3 activities say A1, A2, A3. I can come back from A2 to A1 but when I start A3 and then if user clicks back button then I want to close all previous windows. I tried this in following ways:
Intent intent = new Intent(LoginActivity.this, DashboardActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
but this is not working for me. I am using action-bar activity.I tried to clear task :
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
Above clearing task working for me but it start my A3 with some blank white screen.
Use this
Intent intent = new Intent(LoginActivity.this, DashboardActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish(); // call this to finish the current activity
try this
Intent intent = new Intent(LoginActivity.this, DashboardActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Have you declared android:launchMode="singleTop" for all activities in the manifest? Also, declare it to MainActivity:
<activity android:name=".MyActivity" android:launchMode="singleTop" ... />
Usually, this behaviour happened when we do not declare a launchMode in the manifest. Although you are using Intent.[MODE], then you will need this attribute. See this documentation for more information about launchMode.
Okay so I'm kind of stumped on what to do with this. So I have the MainActivity, and from there an Activity can be launched to DegreePlanActivity, and from there another Activity can be launched to EditDegreePlan. I've got EditDegreePlan set to noHistory in the AndroidManifest. The problem is after they save the EditDegreePlan it launches an Activity to DegreePlan. So if the user presses Back they have to press it twice to get to MainActivity again. I want to get rid of that so they only have to press it once. I'm stumped on how to do this though.
If I set DegreePlanActivity to noHistory then they couldn't press Back to it while in EditDegreePlan.
I've tried overriding onBackPressed method and launching an intent to MainActivity. The problem then is that they have to press Back multiple times to exit the app then.
What should I do?
FLAG_ACTIVITY_CLEAR_TOP clears your Activity stack , you can use the code below:
Intent intent = new Intent(this, Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Remember that this flag clears just Intermediate Activities , for example if you have A,B,C in your Back Stack then going from C Activity to D with this flag this does not clear Back Stack and the Stack would be A,B,C,D but if you go from Activity D to Activity A with this flag , B,C,D Activities will pop up from the stack and you will have just A in the Back Stack .
To remove activity from back stack inside manifest add android:noHistory="true" to your activity inside the manifest file.
See sample below.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activity"
android:versionCode="1"
android:versionName="1.0">
<application android:name="MyApp" android:label="My Application">
<activity android:name=".LoginActivity"
android:noHistory="true"> //add this line to your activity inside manifest
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
simple solution for API >= 15 to API 23
user activity name in intent.
Intent nextScreen = new Intent(currentActivity.this, MainActivity.class);
nextScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(nextScreen);
ActivityCompat.finishAffinity(currentActivity.this);
I would suggest that you use startActivityForResult(), instead of simply startActivity(), when you launch the EditDegreePlan-Activity, as described in the Android tutorials.
In the EditDegreePlan-Activity you then call
setResult(RESULT_OK);
finish();
If you don't expect any data from the EditDegreePlan-Activity, then you don't necessarily have to implement the onActivityResult.
It seems, that you will get the desired behavior if you do not specify any flags at all. The back button would just do the right thing. To get an activity closed from within your code use the finish() method it has the same effect as the user pressing the back button. So you will automatically arrive at DegreePlan when you finish the EditDegreePlan, no need to call any Intents either.
You can call finish before you start your new activity. This will remove the current activity from the stack, so when you press back button from the next activity, the first activity will not be called from the stack history.
Intent i = new Intent(MainActivity.this, NextActivity.class);
finish();
startActivity(i);
Here is your flow:
MainActivity --> DegreePlanActivty --> EditDegreePlan--> DegreePlan --> MainActivty
Override these method inside your "DegreePlan"
public void onBackPressed() {
super.onBackPressed();
Intent goToMainActivity = new Intent(getApplicationContext(), MainActivity.class);
goToMainActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Will clear out your activity history stack till now
startActivity(goToMainActivity);
}
use this to clear the stack :
menuIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent(getContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
You can add flags as follows and start Activity, try below code
Intent i = new Intent(activity, Payment.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP);
activity.startActivity(i);
This code should help you out: It is in Kotlin
private fun verifyIfUserIsLoggedIn(){
val uid = FirebaseAuth.getInstance().uid
if(uid== null){
val intent = Intent(this, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK.or(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}
}
I have an activity that leads to another. i want to make it so that the first activity never opens again once it directs to the other one
clear_top doesnt work. which flag is needed?
This will start a activity with new information and no history
Intent intent = new Intent(MainActivity.this, TargetActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
MainActivity.startActivity(intent);
For your Reference, you can read android documentation : Click Here
You can also use android:noHistory="true" for the first activity in your AndroidManifest.xml. No need for intent flags.
http://developer.android.com/guide/topics/manifest/activity-element.html#nohist
i = new Intent(HomeActivity.this, LoginActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
That should do it.
You could just call finish() in your first Activity after your call to startActivity(...).