OnActivityResult not called when starting a dialog activity and recreating activities












5















I have two activities, A and B. A starts B by calling startActivityForResult(). B has theme @android:style/Theme.Dialog.



Thus, B is displayed "on top of" A, but A is still visible (as B is a dialog).



When both activities are launched, I force recreation by switching to another task and back (I have enabled the "Don't keep activities" option in the android developer settings. I see OnCreate is called on A and B when I return to my task.)



When I click the button in Activity B, it calls setResult() and finish(), but onActivityResult() is not called on A.



The problem does not appear




  • if I do not force recreation of the activities


or




  • if I remove the dialog theme from Activity B.


I tested this on a Google Pixel with Android 9.



Is this expected behavior or a bug in Android?





This is the code I used to test this (Xamarin Android):



[Activity(Label = "@string/app_name")]
public class ActivityA : Activity
{

protected override void OnCreate(Bundle savedInstanceState)
{
Kp2aLog.Log("OnCreate A");
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.yubichall_test);

{
FindViewById<Button>(Resource.Id.btn_yubichall).Text = "Start B";
FindViewById<Button>(Resource.Id.btn_yubichall).Click += (sender, args) =>
{

var chalIntent = new Intent(this, typeof(ActivityB));

StartActivityForResult(chalIntent, 123);

};
}


}

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
Android.Util.Log.Debug("KP2A", "OnActivityResult A: " + requestCode);

}
}
[Activity(Label = "@string/app_name", Theme = "@android:style/Theme.Dialog")]
public class ActivityB : Activity
{

protected override void OnCreate(Bundle savedInstanceState)
{
Kp2aLog.Log("OnCreate B");
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.yubichall_test);

FindViewById<Button>(Resource.Id.btn_yubichall).Text = "Return result to A";

{
FindViewById<Button>(Resource.Id.btn_yubichall).Click += (sender, args) =>
{
SetResult(Result.Ok);
Finish();

};
}


}
}


where the layout yubicall_test is



    <?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<Button android:id="@+id/btn_yubichall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yubi challenge"
/>

<TextView
android:id="@+id/text_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

</LinearLayout>









share|improve this question

























  • My opinion is that it is working correctly, as you have mentioned when coming back from another task onCreate() of both activities are called - so both activities are newly started so Activity B is not created through startActivityForResult() - so it will not be calling onActivityResult() of Activity A while coming from Activity B to A.

    – Raj
    Dec 4 '18 at 16:09













  • This sounds like a bug to me. I was unable to reproduce this behavior using vanilla Android

    – Ben P.
    Dec 4 '18 at 19:31











  • Umm I would like to ask you something about this task of yours, What does it do what is this task, I mean what are you achieving with it.

    – G.hakim
    Dec 5 '18 at 6:29











  • How do you force recreation by switching to another task and back? And why don't you want to keep the activities?

    – tynn
    Dec 5 '18 at 16:07
















5















I have two activities, A and B. A starts B by calling startActivityForResult(). B has theme @android:style/Theme.Dialog.



Thus, B is displayed "on top of" A, but A is still visible (as B is a dialog).



When both activities are launched, I force recreation by switching to another task and back (I have enabled the "Don't keep activities" option in the android developer settings. I see OnCreate is called on A and B when I return to my task.)



When I click the button in Activity B, it calls setResult() and finish(), but onActivityResult() is not called on A.



The problem does not appear




  • if I do not force recreation of the activities


or




  • if I remove the dialog theme from Activity B.


I tested this on a Google Pixel with Android 9.



Is this expected behavior or a bug in Android?





This is the code I used to test this (Xamarin Android):



[Activity(Label = "@string/app_name")]
public class ActivityA : Activity
{

protected override void OnCreate(Bundle savedInstanceState)
{
Kp2aLog.Log("OnCreate A");
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.yubichall_test);

{
FindViewById<Button>(Resource.Id.btn_yubichall).Text = "Start B";
FindViewById<Button>(Resource.Id.btn_yubichall).Click += (sender, args) =>
{

var chalIntent = new Intent(this, typeof(ActivityB));

StartActivityForResult(chalIntent, 123);

};
}


}

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
Android.Util.Log.Debug("KP2A", "OnActivityResult A: " + requestCode);

}
}
[Activity(Label = "@string/app_name", Theme = "@android:style/Theme.Dialog")]
public class ActivityB : Activity
{

protected override void OnCreate(Bundle savedInstanceState)
{
Kp2aLog.Log("OnCreate B");
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.yubichall_test);

FindViewById<Button>(Resource.Id.btn_yubichall).Text = "Return result to A";

{
FindViewById<Button>(Resource.Id.btn_yubichall).Click += (sender, args) =>
{
SetResult(Result.Ok);
Finish();

};
}


}
}


where the layout yubicall_test is



    <?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<Button android:id="@+id/btn_yubichall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yubi challenge"
/>

<TextView
android:id="@+id/text_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

</LinearLayout>









share|improve this question

























  • My opinion is that it is working correctly, as you have mentioned when coming back from another task onCreate() of both activities are called - so both activities are newly started so Activity B is not created through startActivityForResult() - so it will not be calling onActivityResult() of Activity A while coming from Activity B to A.

    – Raj
    Dec 4 '18 at 16:09













  • This sounds like a bug to me. I was unable to reproduce this behavior using vanilla Android

    – Ben P.
    Dec 4 '18 at 19:31











  • Umm I would like to ask you something about this task of yours, What does it do what is this task, I mean what are you achieving with it.

    – G.hakim
    Dec 5 '18 at 6:29











  • How do you force recreation by switching to another task and back? And why don't you want to keep the activities?

    – tynn
    Dec 5 '18 at 16:07














5












5








5








I have two activities, A and B. A starts B by calling startActivityForResult(). B has theme @android:style/Theme.Dialog.



Thus, B is displayed "on top of" A, but A is still visible (as B is a dialog).



When both activities are launched, I force recreation by switching to another task and back (I have enabled the "Don't keep activities" option in the android developer settings. I see OnCreate is called on A and B when I return to my task.)



When I click the button in Activity B, it calls setResult() and finish(), but onActivityResult() is not called on A.



The problem does not appear




  • if I do not force recreation of the activities


or




  • if I remove the dialog theme from Activity B.


I tested this on a Google Pixel with Android 9.



Is this expected behavior or a bug in Android?





This is the code I used to test this (Xamarin Android):



[Activity(Label = "@string/app_name")]
public class ActivityA : Activity
{

protected override void OnCreate(Bundle savedInstanceState)
{
Kp2aLog.Log("OnCreate A");
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.yubichall_test);

{
FindViewById<Button>(Resource.Id.btn_yubichall).Text = "Start B";
FindViewById<Button>(Resource.Id.btn_yubichall).Click += (sender, args) =>
{

var chalIntent = new Intent(this, typeof(ActivityB));

StartActivityForResult(chalIntent, 123);

};
}


}

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
Android.Util.Log.Debug("KP2A", "OnActivityResult A: " + requestCode);

}
}
[Activity(Label = "@string/app_name", Theme = "@android:style/Theme.Dialog")]
public class ActivityB : Activity
{

protected override void OnCreate(Bundle savedInstanceState)
{
Kp2aLog.Log("OnCreate B");
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.yubichall_test);

FindViewById<Button>(Resource.Id.btn_yubichall).Text = "Return result to A";

{
FindViewById<Button>(Resource.Id.btn_yubichall).Click += (sender, args) =>
{
SetResult(Result.Ok);
Finish();

};
}


}
}


where the layout yubicall_test is



    <?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<Button android:id="@+id/btn_yubichall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yubi challenge"
/>

<TextView
android:id="@+id/text_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

</LinearLayout>









share|improve this question
















I have two activities, A and B. A starts B by calling startActivityForResult(). B has theme @android:style/Theme.Dialog.



Thus, B is displayed "on top of" A, but A is still visible (as B is a dialog).



When both activities are launched, I force recreation by switching to another task and back (I have enabled the "Don't keep activities" option in the android developer settings. I see OnCreate is called on A and B when I return to my task.)



When I click the button in Activity B, it calls setResult() and finish(), but onActivityResult() is not called on A.



The problem does not appear




  • if I do not force recreation of the activities


or




  • if I remove the dialog theme from Activity B.


I tested this on a Google Pixel with Android 9.



Is this expected behavior or a bug in Android?





This is the code I used to test this (Xamarin Android):



[Activity(Label = "@string/app_name")]
public class ActivityA : Activity
{

protected override void OnCreate(Bundle savedInstanceState)
{
Kp2aLog.Log("OnCreate A");
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.yubichall_test);

{
FindViewById<Button>(Resource.Id.btn_yubichall).Text = "Start B";
FindViewById<Button>(Resource.Id.btn_yubichall).Click += (sender, args) =>
{

var chalIntent = new Intent(this, typeof(ActivityB));

StartActivityForResult(chalIntent, 123);

};
}


}

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
Android.Util.Log.Debug("KP2A", "OnActivityResult A: " + requestCode);

}
}
[Activity(Label = "@string/app_name", Theme = "@android:style/Theme.Dialog")]
public class ActivityB : Activity
{

protected override void OnCreate(Bundle savedInstanceState)
{
Kp2aLog.Log("OnCreate B");
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.yubichall_test);

FindViewById<Button>(Resource.Id.btn_yubichall).Text = "Return result to A";

{
FindViewById<Button>(Resource.Id.btn_yubichall).Click += (sender, args) =>
{
SetResult(Result.Ok);
Finish();

};
}


}
}


where the layout yubicall_test is



    <?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<Button android:id="@+id/btn_yubichall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yubi challenge"
/>

<TextView
android:id="@+id/text_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

</LinearLayout>






android xamarin.android dialog android-lifecycle






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 1 '18 at 14:56









Radesh

3,91211533




3,91211533










asked Nov 24 '18 at 8:06









PhilippPhilipp

7,066445102




7,066445102













  • My opinion is that it is working correctly, as you have mentioned when coming back from another task onCreate() of both activities are called - so both activities are newly started so Activity B is not created through startActivityForResult() - so it will not be calling onActivityResult() of Activity A while coming from Activity B to A.

    – Raj
    Dec 4 '18 at 16:09













  • This sounds like a bug to me. I was unable to reproduce this behavior using vanilla Android

    – Ben P.
    Dec 4 '18 at 19:31











  • Umm I would like to ask you something about this task of yours, What does it do what is this task, I mean what are you achieving with it.

    – G.hakim
    Dec 5 '18 at 6:29











  • How do you force recreation by switching to another task and back? And why don't you want to keep the activities?

    – tynn
    Dec 5 '18 at 16:07



















  • My opinion is that it is working correctly, as you have mentioned when coming back from another task onCreate() of both activities are called - so both activities are newly started so Activity B is not created through startActivityForResult() - so it will not be calling onActivityResult() of Activity A while coming from Activity B to A.

    – Raj
    Dec 4 '18 at 16:09













  • This sounds like a bug to me. I was unable to reproduce this behavior using vanilla Android

    – Ben P.
    Dec 4 '18 at 19:31











  • Umm I would like to ask you something about this task of yours, What does it do what is this task, I mean what are you achieving with it.

    – G.hakim
    Dec 5 '18 at 6:29











  • How do you force recreation by switching to another task and back? And why don't you want to keep the activities?

    – tynn
    Dec 5 '18 at 16:07

















My opinion is that it is working correctly, as you have mentioned when coming back from another task onCreate() of both activities are called - so both activities are newly started so Activity B is not created through startActivityForResult() - so it will not be calling onActivityResult() of Activity A while coming from Activity B to A.

– Raj
Dec 4 '18 at 16:09







My opinion is that it is working correctly, as you have mentioned when coming back from another task onCreate() of both activities are called - so both activities are newly started so Activity B is not created through startActivityForResult() - so it will not be calling onActivityResult() of Activity A while coming from Activity B to A.

– Raj
Dec 4 '18 at 16:09















This sounds like a bug to me. I was unable to reproduce this behavior using vanilla Android

– Ben P.
Dec 4 '18 at 19:31





This sounds like a bug to me. I was unable to reproduce this behavior using vanilla Android

– Ben P.
Dec 4 '18 at 19:31













Umm I would like to ask you something about this task of yours, What does it do what is this task, I mean what are you achieving with it.

– G.hakim
Dec 5 '18 at 6:29





Umm I would like to ask you something about this task of yours, What does it do what is this task, I mean what are you achieving with it.

– G.hakim
Dec 5 '18 at 6:29













How do you force recreation by switching to another task and back? And why don't you want to keep the activities?

– tynn
Dec 5 '18 at 16:07





How do you force recreation by switching to another task and back? And why don't you want to keep the activities?

– tynn
Dec 5 '18 at 16:07












1 Answer
1






active

oldest

votes


















0














I will try to explain what is happening here. Don't keep activity is used to mimic low memory behaviour on Device, so that when activity goes into background state (onPause, onStop) and gets killed due to memory quench, it will save the required state. Most of the view states are saved by the Android OS itself and is responsible for restoring them when the activity comes back to foreground.



Now as per your scenario, the theme does not seem to be creating any problem in plain Vanilla android when launching activity B. It will pause and stop activity A as it is not a DialogActivity. It just has a theme of Dialog. You should verify this with Xamarin, if it treats Dialog Theme differently then this could be an issue.



Now, when you put Activity B into background (onPause, onStop), it will also get killed as you have Dont keep activity on. But restoring all activities (i.e. B and everything before that with correct View states), is job of Android OS. Even if you see Activity A getting created while restoring state, you will see all the states saved in the bundle getting restored(parameter of onCreate of ActivityA).



Another possibility of this happening could be device specific. As discussed earlier, it doesn't happen on Plain Android but manufacturers (Xiaomi, Samsung) do make modifications to Android OS and this could be very well be your issue. Its a good idea to first develop your app for plain Android and then customize your code for each manufacturer.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456335%2fonactivityresult-not-called-when-starting-a-dialog-activity-and-recreating-activ%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









    0














    I will try to explain what is happening here. Don't keep activity is used to mimic low memory behaviour on Device, so that when activity goes into background state (onPause, onStop) and gets killed due to memory quench, it will save the required state. Most of the view states are saved by the Android OS itself and is responsible for restoring them when the activity comes back to foreground.



    Now as per your scenario, the theme does not seem to be creating any problem in plain Vanilla android when launching activity B. It will pause and stop activity A as it is not a DialogActivity. It just has a theme of Dialog. You should verify this with Xamarin, if it treats Dialog Theme differently then this could be an issue.



    Now, when you put Activity B into background (onPause, onStop), it will also get killed as you have Dont keep activity on. But restoring all activities (i.e. B and everything before that with correct View states), is job of Android OS. Even if you see Activity A getting created while restoring state, you will see all the states saved in the bundle getting restored(parameter of onCreate of ActivityA).



    Another possibility of this happening could be device specific. As discussed earlier, it doesn't happen on Plain Android but manufacturers (Xiaomi, Samsung) do make modifications to Android OS and this could be very well be your issue. Its a good idea to first develop your app for plain Android and then customize your code for each manufacturer.






    share|improve this answer




























      0














      I will try to explain what is happening here. Don't keep activity is used to mimic low memory behaviour on Device, so that when activity goes into background state (onPause, onStop) and gets killed due to memory quench, it will save the required state. Most of the view states are saved by the Android OS itself and is responsible for restoring them when the activity comes back to foreground.



      Now as per your scenario, the theme does not seem to be creating any problem in plain Vanilla android when launching activity B. It will pause and stop activity A as it is not a DialogActivity. It just has a theme of Dialog. You should verify this with Xamarin, if it treats Dialog Theme differently then this could be an issue.



      Now, when you put Activity B into background (onPause, onStop), it will also get killed as you have Dont keep activity on. But restoring all activities (i.e. B and everything before that with correct View states), is job of Android OS. Even if you see Activity A getting created while restoring state, you will see all the states saved in the bundle getting restored(parameter of onCreate of ActivityA).



      Another possibility of this happening could be device specific. As discussed earlier, it doesn't happen on Plain Android but manufacturers (Xiaomi, Samsung) do make modifications to Android OS and this could be very well be your issue. Its a good idea to first develop your app for plain Android and then customize your code for each manufacturer.






      share|improve this answer


























        0












        0








        0







        I will try to explain what is happening here. Don't keep activity is used to mimic low memory behaviour on Device, so that when activity goes into background state (onPause, onStop) and gets killed due to memory quench, it will save the required state. Most of the view states are saved by the Android OS itself and is responsible for restoring them when the activity comes back to foreground.



        Now as per your scenario, the theme does not seem to be creating any problem in plain Vanilla android when launching activity B. It will pause and stop activity A as it is not a DialogActivity. It just has a theme of Dialog. You should verify this with Xamarin, if it treats Dialog Theme differently then this could be an issue.



        Now, when you put Activity B into background (onPause, onStop), it will also get killed as you have Dont keep activity on. But restoring all activities (i.e. B and everything before that with correct View states), is job of Android OS. Even if you see Activity A getting created while restoring state, you will see all the states saved in the bundle getting restored(parameter of onCreate of ActivityA).



        Another possibility of this happening could be device specific. As discussed earlier, it doesn't happen on Plain Android but manufacturers (Xiaomi, Samsung) do make modifications to Android OS and this could be very well be your issue. Its a good idea to first develop your app for plain Android and then customize your code for each manufacturer.






        share|improve this answer













        I will try to explain what is happening here. Don't keep activity is used to mimic low memory behaviour on Device, so that when activity goes into background state (onPause, onStop) and gets killed due to memory quench, it will save the required state. Most of the view states are saved by the Android OS itself and is responsible for restoring them when the activity comes back to foreground.



        Now as per your scenario, the theme does not seem to be creating any problem in plain Vanilla android when launching activity B. It will pause and stop activity A as it is not a DialogActivity. It just has a theme of Dialog. You should verify this with Xamarin, if it treats Dialog Theme differently then this could be an issue.



        Now, when you put Activity B into background (onPause, onStop), it will also get killed as you have Dont keep activity on. But restoring all activities (i.e. B and everything before that with correct View states), is job of Android OS. Even if you see Activity A getting created while restoring state, you will see all the states saved in the bundle getting restored(parameter of onCreate of ActivityA).



        Another possibility of this happening could be device specific. As discussed earlier, it doesn't happen on Plain Android but manufacturers (Xiaomi, Samsung) do make modifications to Android OS and this could be very well be your issue. Its a good idea to first develop your app for plain Android and then customize your code for each manufacturer.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 5 '18 at 11:52









        RoadblockRoadblock

        1,50621632




        1,50621632
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456335%2fonactivityresult-not-called-when-starting-a-dialog-activity-and-recreating-activ%23new-answer', 'question_page');
            }
            );

            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







            Popular posts from this blog

            Wiesbaden

            To store a contact into the json file from server.js file using a class in NodeJS

            Marschland