How to find last activity from which current activity is opened in android?

被刻印的时光 ゝ 提交于 2019-12-28 05:35:47

问题


I have one activity which can be open from more 4 or 5 different activity so i can find from which activity my current activity is called...

If any idea please help me..


回答1:


You might want to add extras to the intent you use to start the activity to indicate where the intent is coming from or what the request is.

For example:

Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra("caller", "MainActivity");
startActivity(intent);

and then your OtherActivity could detect the "caller" in its onCreate:

String caller     = getIntent().getStringExtra("caller");
Class callerClass = Class.forName(caller);



回答2:


Android has a function for it, if you use startActivityForResult:

ComponentName prev = this.getCallingActivity();




回答3:


Put some extras to intent for the different- different activities

eg:

startActivity(new Intent(this, otherActivity.class).putExtra("from" , "previousActivity"));

and get extras in the current activity as

string act = getIntent().getStringExtra("from");

so "act" will indicate you the calling Activity.




回答4:


Create an Interfare And put Values for your Activities Then pass these Values in Intent And get in Child Activity take a look at this piece of code

public interface ActivityRefs {
            public static final int ACTIVITY_1 = 1;
            public static final int ACTIVITY_2 = 2;
            public static final int ACTIVITY_3 = 3;
}

Create Intent and pass value like this in Calling activity

intent.putExtra("callingActivity", ActivityRefs.ACTIVITY_1);

Then in Called activity

int callingActivityName = getIntent().getIntExtra("callingActivity", 0);

        switch (callingActivityName) {
        case ActivityConstants.ACTIVITY_1:
            // Activity3 is started from Activity1
            break;
        case ActivityConstants.ACTIVITY_2:
            // Activity3 is started from Activity2
            break;
        }



回答5:


I have solved it, in my case we are on ActivityA to ActivityB then want to go back to ActivityA programmatically, here my code

ActivityA :

String currentActivity;
void onCreate(...){
...
currentActivity = this.getClass().getName();
...
}

Then on your intent (still in ActivityA) :

Intent intent = new Intent(this, ActivityA.class);
intent.putExtra("from", currentActivity);
startActivity(intent);
this.finish();

Now on ActivityB:

Class fromClass;
void onCreate(...){
...
String String from = getActivity().getIntent().getStringExtra("from");
try {
    fromClass = Class.forName(from);
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}
...
}

Then on your intent(still in ActivityB) for back to ActivityA:

Intent intent = new Intent(this, fromClass);
startActivity(intent);
this.finish();


来源:https://stackoverflow.com/questions/5467922/how-to-find-last-activity-from-which-current-activity-is-opened-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!