Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare activity from getCallingActivity to another

I would like to compare the calling activity to others to know which one called the current activity. I tried:

getCallingActivity().getClassName().toString().equals(MainActivity.class.toString())

It doesn't work, except by passing a value in the calling Intent, how can we compare classes using getCallingActivity() or getCallingPackage()?

like image 470
Jerec TheSith Avatar asked Apr 10 '13 15:04

Jerec TheSith


People also ask

How can we call method in activity from non activity class?

onCreate(savedInstanceState); setContentView(R. layout. main2); DataClass dc = new DataClass(); dc. show(); } public void call(ArrayList<String> arr) { // Some code... } }

How can I get call activity in Android?

If you start the activity with startActivityForResult(Intent, int) , then you can get calling activity by getCallingActivity(). getClassName() .

What is main activity in Android?

Typically, one activity in an app is specified as the main activity, which is the first screen to appear when the user launches the app. Each activity can then start another activity in order to perform different actions.


1 Answers

You can compare the calling activity in the following manner if the activity is started for result from another activity.

  if (getCallingActivity().getClassName().equals(MainActivity.class.getName())) {

            /* here the main activity is calling activity*/

     }else{

             /*other activity is calling activity*/
  }
like image 140
Gopal Singh Sirvi Avatar answered Oct 18 '22 13:10

Gopal Singh Sirvi