Launching an activity at its current state

2019-01-25 22:43发布

问题:

I am trying to make a task switcher and I succeeded in it. My only problem is that when I launch activities, they are relaunched as they were new activities ( for instance, I am writing an email, I press home and go into my activity,launch email, and then the app launch the email bout goes back to the inbox and the email is lost) So that's not true multitasking.

Here are my steps:

1) getting all the running apps:

List<ActivityManager.RunningTaskInfo> allTasks = activityManager.getRunningTasks(30);

2) getting the intent:

for (ActivityManager.RunningTaskInfo aTask : allTasks) {
        Intent i = new Intent(Intent.ACTION_MAIN);
        i.setComponent(aTask.baseActivity);
(...)

3) Launching the application when clicking on the button:

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED).addCategory(Intent.CATEGORY_LAUNCHER);
    monthis.startActivity(intent);

`

What is wrong with this code? Should I do something different to get it?

Thank a lot for any answer.

回答1:

When creating the Intents you should not use Intent.FLAG_ACTIVITY_NEW_TASK, you should use FLAG_ACTIVITY_REORDER_TO_FRONT.



回答2:

Sorry if I made mistakes in my explanation, I am quite a "noob" and just tell here my experience to improve the result of people searching for the same answer than me.

In fact, I had to Use intent.setFlag(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_REORDER_TO_FRONT) for the best result. Replacing was not the best Idea.

Not using FLAG_ACTIVITY_NEW_TASK make the email application launch when I wanted to launch my own application. Because email was "linked" with the same task than my Application.

But Lucas, I keep your answer as the best.



回答3:

I think I found the answer. Let me tell what i have done in simple words,

Suppose i am having two activities activity1 and activity2 and i am navigating from activity1 to activity2(i have done some works in activity2) and again back to activity 1 by clicking on a button in activity1. Now at this stage I wanted to go back to activity2 and i want to see my activity2 in the same condition when I last left activity2.

For the above scenario what i have done is that in the manifest i made some changes like this:

<activity android:name=".activity2"
          android:alwaysRetainTaskState="True"
          android:launchMode="singleInstance">
</activity>

And in the activity1 on the button click event i have done like this:

Intent intent=new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.setClassName(this,"com.mainscreen.activity2");
startActivity(intent);

And in activity2 on button click event i have done like this:

Intent intent=new Intent();
intent.setClassName(this,"com.mainscreen.activity1");
startActivity(intent);

Now what will happen is that whatever the changes we have made in the activity2 will not be lost, and we can view activity2 in the same state as we left previously.

I believe this is the answer and this works fine for me. Correct me if i am wrong.