Where should I be using getIntent() in the Android lifecycle
I have a bug in my app which I thought I knew how to resolve, but now I've thought more about what is happening I'm not sure I do know the fix.
My app receives an incoming intent from a calling third party app as a string. That string can be sent as a SEND
intent or a VIEW
intent.
All of that works fine unless the app is already running...
This is what is happening:
- My app is not running (not listed in the running apps view)
- Share is clicked in another (third party) app and my app is selected to receive the shared text (text1).
- My app opens and the text is displayed (and processed) as expected.
- The user switches back to the third party app and shares some different text (text2) and my app is selected to receive this new text.
- My app opens, but the original text (text1) is still displayed.
At this point I thought that the bug was because I am reading the intent in onCreate()
and then displaying and processing it. My thinking was that as the app is already running onCreate()
is not being called when the app is shown the second time as we jump into the lifecycle at onResume()
.
However, if I continue the test as follows:
- Without exiting my app the user switches back to the third party app again and again shares the same piece of second text (text2) with my app.
- My app is displayed again but this time correctly shows and processes the second text.
How can this be, as the app is still running, surely onCreate()
is still not going to be called!
I thought that the fix was going to be simply to move the getIntent()
call into onResume()
(or onStart()
?) But now I'm not sure about this. Is this right thing to do?
android android-intent android-activity android-lifecycle
add a comment |
I have a bug in my app which I thought I knew how to resolve, but now I've thought more about what is happening I'm not sure I do know the fix.
My app receives an incoming intent from a calling third party app as a string. That string can be sent as a SEND
intent or a VIEW
intent.
All of that works fine unless the app is already running...
This is what is happening:
- My app is not running (not listed in the running apps view)
- Share is clicked in another (third party) app and my app is selected to receive the shared text (text1).
- My app opens and the text is displayed (and processed) as expected.
- The user switches back to the third party app and shares some different text (text2) and my app is selected to receive this new text.
- My app opens, but the original text (text1) is still displayed.
At this point I thought that the bug was because I am reading the intent in onCreate()
and then displaying and processing it. My thinking was that as the app is already running onCreate()
is not being called when the app is shown the second time as we jump into the lifecycle at onResume()
.
However, if I continue the test as follows:
- Without exiting my app the user switches back to the third party app again and again shares the same piece of second text (text2) with my app.
- My app is displayed again but this time correctly shows and processes the second text.
How can this be, as the app is still running, surely onCreate()
is still not going to be called!
I thought that the fix was going to be simply to move the getIntent()
call into onResume()
(or onStart()
?) But now I'm not sure about this. Is this right thing to do?
android android-intent android-activity android-lifecycle
What launch mode do you have set on your activity?android:launchMode
developer.android.com/guide/topics/manifest/activity-element
– Dom
Nov 21 '18 at 23:47
add a comment |
I have a bug in my app which I thought I knew how to resolve, but now I've thought more about what is happening I'm not sure I do know the fix.
My app receives an incoming intent from a calling third party app as a string. That string can be sent as a SEND
intent or a VIEW
intent.
All of that works fine unless the app is already running...
This is what is happening:
- My app is not running (not listed in the running apps view)
- Share is clicked in another (third party) app and my app is selected to receive the shared text (text1).
- My app opens and the text is displayed (and processed) as expected.
- The user switches back to the third party app and shares some different text (text2) and my app is selected to receive this new text.
- My app opens, but the original text (text1) is still displayed.
At this point I thought that the bug was because I am reading the intent in onCreate()
and then displaying and processing it. My thinking was that as the app is already running onCreate()
is not being called when the app is shown the second time as we jump into the lifecycle at onResume()
.
However, if I continue the test as follows:
- Without exiting my app the user switches back to the third party app again and again shares the same piece of second text (text2) with my app.
- My app is displayed again but this time correctly shows and processes the second text.
How can this be, as the app is still running, surely onCreate()
is still not going to be called!
I thought that the fix was going to be simply to move the getIntent()
call into onResume()
(or onStart()
?) But now I'm not sure about this. Is this right thing to do?
android android-intent android-activity android-lifecycle
I have a bug in my app which I thought I knew how to resolve, but now I've thought more about what is happening I'm not sure I do know the fix.
My app receives an incoming intent from a calling third party app as a string. That string can be sent as a SEND
intent or a VIEW
intent.
All of that works fine unless the app is already running...
This is what is happening:
- My app is not running (not listed in the running apps view)
- Share is clicked in another (third party) app and my app is selected to receive the shared text (text1).
- My app opens and the text is displayed (and processed) as expected.
- The user switches back to the third party app and shares some different text (text2) and my app is selected to receive this new text.
- My app opens, but the original text (text1) is still displayed.
At this point I thought that the bug was because I am reading the intent in onCreate()
and then displaying and processing it. My thinking was that as the app is already running onCreate()
is not being called when the app is shown the second time as we jump into the lifecycle at onResume()
.
However, if I continue the test as follows:
- Without exiting my app the user switches back to the third party app again and again shares the same piece of second text (text2) with my app.
- My app is displayed again but this time correctly shows and processes the second text.
How can this be, as the app is still running, surely onCreate()
is still not going to be called!
I thought that the fix was going to be simply to move the getIntent()
call into onResume()
(or onStart()
?) But now I'm not sure about this. Is this right thing to do?
android android-intent android-activity android-lifecycle
android android-intent android-activity android-lifecycle
edited Nov 22 '18 at 16:00
Fat Monk
asked Nov 21 '18 at 22:57
Fat MonkFat Monk
750934
750934
What launch mode do you have set on your activity?android:launchMode
developer.android.com/guide/topics/manifest/activity-element
– Dom
Nov 21 '18 at 23:47
add a comment |
What launch mode do you have set on your activity?android:launchMode
developer.android.com/guide/topics/manifest/activity-element
– Dom
Nov 21 '18 at 23:47
What launch mode do you have set on your activity?
android:launchMode
developer.android.com/guide/topics/manifest/activity-element– Dom
Nov 21 '18 at 23:47
What launch mode do you have set on your activity?
android:launchMode
developer.android.com/guide/topics/manifest/activity-element– Dom
Nov 21 '18 at 23:47
add a comment |
1 Answer
1
active
oldest
votes
The core of the issue is the fact that your Activity
is already on top of the activity stack when the second Intent
is being fired.
The launch mode will matter here.
What you can do is, set the launchMode to singleTop
, and then get the intent in onNewIntent() method.
That is a good way of handling such scenarios. Please read the link above and it will make things very clear.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421612%2fwhere-should-i-be-using-getintent-in-the-android-lifecycle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The core of the issue is the fact that your Activity
is already on top of the activity stack when the second Intent
is being fired.
The launch mode will matter here.
What you can do is, set the launchMode to singleTop
, and then get the intent in onNewIntent() method.
That is a good way of handling such scenarios. Please read the link above and it will make things very clear.
add a comment |
The core of the issue is the fact that your Activity
is already on top of the activity stack when the second Intent
is being fired.
The launch mode will matter here.
What you can do is, set the launchMode to singleTop
, and then get the intent in onNewIntent() method.
That is a good way of handling such scenarios. Please read the link above and it will make things very clear.
add a comment |
The core of the issue is the fact that your Activity
is already on top of the activity stack when the second Intent
is being fired.
The launch mode will matter here.
What you can do is, set the launchMode to singleTop
, and then get the intent in onNewIntent() method.
That is a good way of handling such scenarios. Please read the link above and it will make things very clear.
The core of the issue is the fact that your Activity
is already on top of the activity stack when the second Intent
is being fired.
The launch mode will matter here.
What you can do is, set the launchMode to singleTop
, and then get the intent in onNewIntent() method.
That is a good way of handling such scenarios. Please read the link above and it will make things very clear.
answered Nov 22 '18 at 0:48
codeFoodcodeFood
8661111
8661111
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421612%2fwhere-should-i-be-using-getintent-in-the-android-lifecycle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What launch mode do you have set on your activity?
android:launchMode
developer.android.com/guide/topics/manifest/activity-element– Dom
Nov 21 '18 at 23:47