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

前端 未结 5 616
野性不改
野性不改 2020-12-05 09:46

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

相关标签:
5条回答
  • 2020-12-05 09:51

    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);
    
    0 讨论(0)
  • 2020-12-05 09:53

    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.

    0 讨论(0)
  • 2020-12-05 09:55

    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;
            }
    
    0 讨论(0)
  • 2020-12-05 10:03

    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();
    
    0 讨论(0)
  • 2020-12-05 10:10

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

    ComponentName prev = this.getCallingActivity();

    0 讨论(0)
提交回复
热议问题