s011208
s011208

Reputation: 405

about android intent action

Current I am working in intent; however, I got some problems.

The thing is, I have two apps, A and B. The lunchmode of B is android:launchMode="singleTop".

Now, I want to pass an intent from A to B, said "sdcard/Android"(a directory path). After that, A will be finished and B will be created/resumed/onNewintent. At the first time, B will receive an intent string "sdcard/Android", which is exactly I want.

Then I press the home button to the launcher and open A again then pass a new data, said "sdcard/Music", to B. However, the problems is occured, B will not get the string "sdcard/Music", instead, B's intent data is still "sdcard/Android".

I expect the second time A pass the data to B, the onNewintent method will be called in B. Are there any wrongs? How can I pass the correct data to B in the second time?

@Override
public void onCreate(Bundle savedInstanceState) {
    onNewIntent(getIntent());
}

@Override
public void onNewIntent(Intent intent)
{
    Log.i("TAG", intent.getStringExtra("path"));
}

I know that I should overwrite the onNewIntent. At first time, the B will get into the onCreate method. On the second time, I expectd it get into the onNewIntent method; however, it get into the onResumed method..!

Upvotes: 0

Views: 681

Answers (1)

Squonk
Squonk

Reputation: 48871

The method onNewIntent(...) isn't called the first time for your Activity B it is only called on the second and further times that Activity B is started.

You can 're-write' the original Intent by doing something like the following...

@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);
}

@Override
protected void onResume() {
    super.onResume();
    handleIntent(getIntent());
}

private void handleIntent(Intent intent) {
    // The intent parameter here will be the original `Intent` the first
    // time Activity B is started. It will be the new Intent after that
    // as onNewIntent(...) re-writes it with the call to setIntent(...)
}

Upvotes: 2

Related Questions