How to make single copy of an activity over entire application - android

Here i have opened activity in following order
A->B->C->D
now i just want to re-launch activity B so that order becomes:
A->C->D->B
can anyone please tell me that how can i achieve this state in android.
Thanks in advance.

Do this in your manifest file for activity B:
<activity
android:name=".B"
android:launchMode="singleTask"/>

now you have A B C D in your app
try the following below:
Intent intent = new Intent(xxx.this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
also check file in here

Related

How to close all activities in a task without clearing the entire backstack?

Let's say I have activities:
First ----------------- Open
A -> B -> C -> D -> E -> F
I want to call finish on activities C,D,E when F is opened, leaving A, B in the back stack such that when a user hits back from F they arrive at B, and so forth.
I have seen tons of examples of clearing the back stack, or pulling one activity from the back stack and clearing the rest, but every time I try to close a Task I end up with an empty back stack and the back button falls out of the app. I wish there were more info on this specific case, and what the best practice for this is (C,D,E,F is a workflow with API side effects that should not be re-entered once leaving, but falling out of the app on back pressed is also not what I want.)
One option is using startActivityForResult() from B, so C will be opened. Then open the next Activities using FLAG_ACTIVITY_FORWARD_RESULT until you reach F.
Override onBackPressed() in F to call setResult(), so users will go back to B. If you don't call startActivityForResult() again, the user will never again be able to reach Activitys C to F.
You can find a detailed example for using FLAG_ACTIVITY_FORWARD_RESULT in my
answer to another SO post
Suppose you have following A->B->C->D->E activities and when you backpressed from activity E, you does'nt want to see activities C & D.
Then you've to pass intent A to B by using this,
Intent i=new Intent(A.this,B.class);
startActivity(i);
Then you've to pass intent B to C by using this,
Intent i=new Intent(B.this,C.class);
startActivity(i);
Then you've to pass intent C to D by using this,
Intent i=new Intent(C.this,D.class);
startActivity(i);
finish();
Then you've to pass intent D to E by using this,
Intent i=new Intent(D.this,E.class);
startActivity(i);
finish();
At final you get Activity E and after that when you backpressed from E ,you able to go at Activity B and after that A.
Thankyou Happy Coding
The best workaround I could find was flagging the final task with FLAG_ACTIVITY_TASK_ON_HOME which just happens to be the task that was in the backstack at the point of departure, in my case. This isn't exactly an answer to the question, but if you are trying to clear a volatile workflow from the backstack and you don't want the user to "fall out" of your app on backpress subsequently, this is a viable option. See thread on the topic here: Android - Clearing Navigation Backstack
Edit: This code below creates separate application instances which I just noticed; not sure if there is a way to prevent that.
The solution I came up with was using taskAffinity, which you set in the manifest xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.example.app">
<activity
android:name=".activities.SplashActivity"
android:launchMode="singleTop"
android:taskAffinity="#string/affinity_main">
</activity>
<activity
android:name=".activities.MainActivity"
android:launchMode="singleTop"
android:taskAffinity="#string/affinity_main">
</activity>
<activity
android:name=".activities.WorkflowStepOneActivity"
android:launchMode="singleTop"
android:taskAffinity="#string/affinity_main">
</activity>
<activity
android:name=".activities.WorkflowStepTwoActivity"
android:launchMode="singleTop"
android:taskAffinity="#string/affinity_workflow">
</activity>
<activity
android:name=".activities.WorkflowStepThreeActivity"
android:launchMode="singleTop"
android:taskAffinity="#string/affinity_workflow">
</activity>
<activity
android:name=".activities.LeaveWorkflowActivity"
android:launchMode="singleTop"
android:taskAffinity="#string/affinity_main">
</activity>
</manifest>
Starting the app the main affinity presumably collects any activities that are started in a task without a declared affinity, but when I want to start a new affinity for my workflow I add new task to the intent:
In MainActivity, or anywhere I call the workflow:
public void startWorkflow() {
Intent intent = new Intent(MainActivity.this,
WorkflowStepOneActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
In intermediate workflow steps we do nothing special:
public void startWorkflowTwo() {
Intent intent = new Intent(MainActivity.this,
LeaveWorkflowActivity.class);
startActivity(intent);
}
Finally, we call finish on the Affinity from our LAST workflow activity, after firing an intent for the post-workflow activity:
public void startWorkflowCompleted() {
Intent intent = new Intent(MainActivity.this,
WorkflowCompletedActivity.class);
startActivity(intent);
finishAffinity();
}
This means our stack will look like this:
SplashActivity -> MainActivity -> LeaveWorkflowActivity

Start another app activity removing it from recents list

I'm trying to start an activity of an app (that is not mine and which code I don't know and/or can't edit), from my app.
What I want to do, is that after starting that activity (let's call it OtherAppActivity) neither my activity (let's call it MyAppActivity) nor OtherAppActivity remain shown in the recent apps list.
My current code:
AndroidManifest.xml
<activity
android:name=".activities.Launch"
android:noHistory="true"
android:exported="true"
android:excludeFromRecents="true"
android:launchMode="singleInstance"
android:theme="#style/EmptyActivity">
MyAppActivity.java
// Intent to launch OtherAppActivity
Intent intent = new Intent();
intent.setClassName(PKG, ACTIVITY);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivityForResult(intent, 88);
I have done a lot of search, but none of the answers I have found, have worked for me.
I hope you understand what I want to do, and can help me. Thanks in advance.
For your app, try adding android:excludeFromRecents="true" to your application tag in the manifest file and for the intent try setting this flag:
intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

startActivity(intent) it doesn't working ? when my DemoActivity use android:launchMode="singleTask",

This is my AndroidManifest.xml
<activity android:name=".ui.activity.ERPWebContainerActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="#style/Theme.Eallcn.Fullscreen">
</activity>
When I step into a new Demo1Activity, I deal with some data, Step to Demo2activity from Demo1Activity.
when I in the Demo2activity activity. I use this method: startActivity() doesn't work.
Intent intent = new Intent(this, ERPWebContainerActivity.class);
intent.putExtra("waitUploadImageLists", waitUploadImageLists);
startActivity(intent);
finish();
You can use android:launchMode="singleInstance"which work almost same.
but still if your requirement is to use singleTask only then you can use that in this way:
android:launchMode="singleInstance"
Just remove the launchMode="singleTask" and set FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP flag whenever call an intent to your activity
You can override this method, you can get the Intent data!
why ? because the activity didn't finish, When i startActivity form Demo2activit, It does't go to onCreate method!
you can get the reslove way: you can see Android: lunchMode
enter link description here
Try in this order.
finish();
Intent intent = new Intent(/*intent parameters, but as I can't copy and paste it from a picture then... */);
startActivity(intent);

Could an activity with single task flag be cleared via clear top?

Assume that I have a launcher activity A which has singleTask launch mode. Now imagine that A starts Activity B like;
Intent intent = new Intent(this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intent, REQ_ACCOUNT_ACTIVITY);
In this case it seems like A's not destroyed. I wonder if it's correct or did I something wrong?
You can achieve this by using below attribute in AndroidMenifest file
android:finishOnTaskLaunch="true"
<application
...
>
<activity
android:finishOnTaskLaunch="true"
android:launchMode="singleTask"
...>
</activity>
</application>
If you not want to kept new activity in the history stack. Use below one_
FLAG_ACTIVITY_NO_HISTORY
//Actvity B is not in BackStack if we set FLAG_ACTIVITY_NO_HISTORY flag
Intent intent = new Intent(this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivityForResult(intent, REQ_ACCOUNT_ACTIVITY);
As soon as the user navigates away from Activity B, the activity is finished. This may also be set with the noHistory attribute.
For more -> Tasks and Back Stack
It's been a while but I came across my own question when I was wandering over here.
The activity A was not getting deleted because the flags are set for the activity B. And when I call this code, there's no such activity called B in the task stack already. So effectively, nothing to clear on top of B.
Hope this helps.

How to kill an Android activity when leaving it so that it cannot be accessed from the back button?

In an given Android activity, I would like to start a new activity for the user at some point. Once they leave the first activity and arrive at the second, the first activity is stale and I want to remove it completely so it can not be accessed again from the back button.
How is the best way to accomplish this? How do I kill or destroy this activity immediately after the user has launched the new activity?
You just need to call finish()
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish();
Setting android:noHistory="true" on the activity in your manifest will remove an activity from the stack whenever it is navigated away from. see here
you can use:
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
You can also add android:noHistory="true" to your Activity tag in AndroidManifest.xml.
<activity
...
android:noHistory="true">
</activity>
Yes, all you need to do is call finish() in any Activity you would like to close.
Write this in each "new activity" after you initialized your new intent->
Intent i = new Intent(this, yourClass.class);
startActivity(i);
finish();
Finally, I got a solution!
My Context is:- I want disconnect socket connection when activity destroyed, I tried to finish() activity but it didn't work me, its keep connection live somewhere.
so I use android.os.Process.killProcess(android.os.Process.myPid());
its kill my activity and i used android:excludeFromRecents="true"
for remove from recent activity .
Add this attribute to you activity in manifest file.
android:noHistory="true"
You've below options to remove an activity from the back stack for a particular task.
Call finish() method just after startActivity() like this:
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
finish();
Add an Intent flag like so:
startActivity(new Intent(FirstActivity.this, SecondActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY));
Add noHistory=true in your manifest file for that particular activity
You just need to use below code when launching the new activity.
startActivity(new Intent(this, newactivity.class));
finish();
In Kotlin, you may use this
startActivity(Intent(context, newActivity::class.java))
finish()
Or you can use this also
val intent = Intent(context, newActivity::class.java))
startActivity(intent)
finish()

Categories

Resources