开发者

How to find out which activity has been loaded

开发者 https://www.devze.com 2023-02-11 13:51 出处:网络
I have 3 activ开发者_运维技巧ities. A, B and C. From A to C and B to C. How can i from Activity C find out which activity was loaded previously and refereed to this Activity.You could handle this thro

I have 3 activ开发者_运维技巧ities. A, B and C. From A to C and B to C. How can i from Activity C find out which activity was loaded previously and refereed to this Activity.


You could handle this through intent bundles. Basically in Activity A or B you launch Activity C as follows:

Intent launchIntent = new Intent(this, ActivityC.class);
launchIntent.putExtra("originActivity", this.getClass().getName());

In Activity C, you retrieve it like

public class ActivityC extends Activity{
   onCreate(...){
      Intent callingIntent = getIntent();

      String originActivity = callingIntent.getStringExtra("originActivity");

   }
}

Now I passed the activity name as string, you may include it in some more convenient way, using constants or something like that. You can look it up here.


If you start your activity C with startActivityForResult instead of startActivity, you have access to the calling Activity:

Start Activity C like this:

Intent intent = new Intent(this, C.class);
int requestCode = 1; // Or some other integer
startActivityForResult(intent, requestCode);

in Activity C:

onCreate(...) {
  String callingClassName = getCallingActivity().getClass().getSimpleName();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消