How to check if an activity is the last one in the

2019-01-03 23:22发布

I want to know if user would return to the home screen if he exit the current activity.

9条回答
Juvenile、少年°
2楼-- · 2019-01-03 23:48

I'm going to post the comment of @H9kDroid as the best answer here for people that have a similar question.

You can use isTaskRoot() to know whether the activity is the root of a task.

I hope this helps

查看更多
We Are One
3楼-- · 2019-01-03 23:53

I've created a base class for all my activities, extending the AppCompatActivity, and which has a static counter:

public abstract class BasicActivity extends AppCompatActivity {
    private static int activityCounter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ++activityCounter;
        ...
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        --activityCounter;
        if(activityCounter==0) {
            // Last instance code...
        }
    }

    public boolean isLastInstance() { return (activityCounter==1); }
}

This has worked well enough, so far; and regardless of API version. It requires of course that all activities extends this base class - which they do, in my case.

Edit: I've noticed one instance when the counter goes down to zero before the app completely exits, which is when the orientation is changed and only one activity is open. When the orientation changes, the activity is closed and another is created, so onDestroyed is called for the last activity, and then onCreate is called when the same activity is created with the changed orientation. This behaviour must be accounted for; OrientationEventListener could possibly be used.

查看更多
SAY GOODBYE
4楼-- · 2019-01-03 23:57

One way to keep track of this is to include a marker when you start a new activity and check if the marker exists.

Whenever you start a new activity, insert the marker:

newIntent=new Intent(this, NextOne.class);
newIntent.putExtra(this.getPackageName()+"myself", 0);
startActivity(newIntent);

And you can then check for it like this:

boolean islast=!getIntent().hasExtra(this.getPackageName()+"myself")
查看更多
够拽才男人
5楼-- · 2019-01-03 23:58

Android implements an Activity stack, I suggest you read about it here. It looks like all you want to do though is retrieve the calling activity: getCallingActivity(). If the current activity is the first activity in your application and the application was launched from the home screen it should (I assume) return null.

查看更多
你好瞎i
6楼-- · 2019-01-04 00:03

The Problem with sandrstar's solution using ActivityManager is: you need a permission to get the tasks this way. I found a better way:

getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)

The Activity on the Stack bottom should allways get this category by default while other Activities should not get it.
But even if this fails on some devices you can set it while starting your Activity:

Intent intent = new Intent(startingActivity, SomeActivityClass.class);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
activity.startActivity(intent);
查看更多
Lonely孤独者°
7楼-- · 2019-01-04 00:03

The one thing that missed here, is the "Home key" click, when activated, you can't detect this from your activity, so it would better to control activity stack programmatically with handling "Back key" press and moving to required activity or just doing necessary steps.

In addition, you can't be sure, that starting your activity from "Recent Activity" list can be detected with presetting some extra data into intent for opening activity, as it being reused in that case.

查看更多
登录 后发表回答