3

How can I know if previous Activity I navigated from to the current Activity belong to my application or not? meaning did the last screen was part of my application/written in my manifest file or it's a home screen or maybe a call screen?

is there a way to know that without passing info via the Intent?

Thanks.

Pragnani
  • 20,075
  • 6
  • 49
  • 74
Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • 1
    You might be able to figure out using ActivityManager to get the list of running tasks, but this requires the GET_TASKS permission and unless your app has some obvious functionality related to managing tasks, users will find it strange that you request this permission. Why do you need to know if the last screen was one of yours? – Karakuri Mar 31 '13 at 21:33
  • There are really 3 choices: from Home, call screen or search isn't it? – Hoan Nguyen Mar 31 '13 at 22:02

2 Answers2

1

I do not have enough reputation to comment on an answer, but for some reason I am able to answer questions. I would think it would be the other way around :S (I'm new here)

To piggy back on Pragnani's answer, I was also looking for the previous activity instead of the current one. To answer Alexander Malakhov's comment:

topActivity is always from current package (if it's not a service etc). Do you have an example when that's not the case ? I.e. top RunningTask(0).topActivity gives you current activity, not previous one. –

simply change:

String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();
String className = am.getRunningTasks(1).get(0).topActivity.getClassName();

to

String packageName = am.getRunningTasks(2).get(1).topActivity.getPackageName();
String className = am.getRunningTasks(2).get(1).topActivity.getClassName();
Community
  • 1
  • 1
Mackarous
  • 1,467
  • 1
  • 11
  • 8
-1

Try this

I think you can check that by checking the package name of your app with the package name of the activity that you from where you have navigated here

we can get the package name previous activity from the Activity Stack

ActivityManager am = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);

here you will get your package name ,

String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();

String className = am.getRunningTasks(1).get(0).topActivity.getClassName();

And then get your current package name like this

String currentpackage = getApplicationContext().getPackageName();

Check whether two packages are same

i.e

  if(packageName.equals(currentpackage))
       {
        Toast.makeText(context,"Same Application package",1).show();
       }

it uses the permission in the manifest

<uses-permission android:name="android.permission.GET_TASKS" />

I hope this will help you

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • 1
    `topActivity` is always from current package (if it's not a service etc). Do you have an example when that's not the case ? I.e. top RunningTask(0).topActivity gives you current activity, not previous one. – Alexander Malakhov Nov 21 '13 at 08:26