3

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();
}
aamiri
  • 2,420
  • 4
  • 38
  • 58
  • Can you add the code that you are using to set the string from the intent? – theisenp Jun 28 '11 at 19:02
  • the code is up now. It's not actually a string, because you can't use switch on a string. It's an enum that represents a string. I appreciate any help you can give. – aamiri Jun 29 '11 at 14:47

1 Answers1

9

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.

Steve Prentice
  • 23,230
  • 11
  • 54
  • 55