aamiri
aamiri

Reputation: 2440

Android: Changing extras to an intent

I have an app that goes between many different activities. One activity, my main activity, has it's behavior dependent on which activity, within the app, launched the main activity. I thought i could log this by having every other activity put an extra called "launcehdFrom" into the intent, which contains a string that has the name of hte activity that called the main activity. The problem i ran into is that once that value has been set it can't be overriden by another activity. I havne't found a good straightforward method for doing this. Any advice??

The following code is called from onResume() in my main activity:

    private void processIntentRequest(){
    Intent intent = getIntent();
    ProcessIntentRequestType caller = (ProcessIntentRequestType)intent.getSerializableExtra("launchedFrom");
    switch(caller){
        case startUpActivity:
            load(this.myObj);
            break;

        case otherActivity:
            int uri = integer.parseInt(this.getIntent().getExtras().getString("uri"));          
            load(this.myObj, uri);
            break;

        default:
            load(this.myObj, 1);
    }

This is the code that launches the main activity the first time:

public void launchMainActivity(Obj myObj){
    Intent launchMain = new Intent(this, mainActivity.class);
    login.putExtra("launchedFrom", ProcessIntentRequestType.startupActivity);
    startActivity(launchMain);
}

This is the code that launches the main activity from some other activity after it has been loaded atleast once by the startup activity:

protected void launchMainActivity(Obj myObj, HelperObj helper) {
    String uri = helper.uri;
    Intent mainActivity = new Intent(this, MainActivity.class);
    mainActivity.putExtra("uri", uri);
    mainActivityputExtra("launchedFrom", ProcessIntentRequestType.otherActivity);
    startActivity(mainActivity.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
                    .addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
    finish();
}

Upvotes: 3

Views: 4125

Answers (1)

Steve Prentice
Steve Prentice

Reputation: 23514

You are using FLAG_ACTIVITY_SINGLE_TOP when calling startActivity which has some notable behavior. In this case, override onNewIntent and call setIntent from there. After that, the platform will call onResume and your call to getIntent will return the new intent.

Upvotes: 9

Related Questions