Android Open MainActivity after click back button in Notification Activity












1














I am developing one application, which will show a list of Card View inside a Recycler view in MainActivity. While clicking the Card view it will launch different activity. The flow is
SplashScreenActivity -> MainActivity(Recycler View) -> DetailsActivity
The flow is working fine if we launch the application by clicking app icon.



As part of this app, I am sending FCM notifications to the app. Whenever notification is received DetailsActivity will be launched, during that time MainActivity will not be in the stack. I am staring the MainActivity whenever backbutton of Details Activity is being pressed.If DetailsActivity is not called from Notification then I am not calling MainActivity again.



Below is the code snippet I have done to handle back button in Details Activity.



@Override
public void onBackPressed() {
if (isFromNotification) {
System.out.println("Start from Notification");
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
super.onBackPressed();
startActivity(intent);
}else {
System.out.println("Start from Normal Mode");
super.onBackPressed();
}
}


I have two issues here.



i) When FCM is received Notification is viewed in DetailsActivity. After pressing the back button MainActivity is launched and DetailsActivity is not killed. After Pressing the back button from MaainActivity again Details Activity is launched.



ii) Whenever MainActivity is launched by after receiving notification (By Clicking back button of Details Activity), recyclerview elements are not clickable.



I tried my best to explain the issue, your help is really appreciated !!!










share|improve this question






















  • for first issue you can just add a finish(); after starting the mainactivity in your if condition. It will kill the detailsActivity on backpressed. For second issue, we might need more details on code or xml file to look into.
    – ManishPrajapati
    Nov 11 at 6:21












  • added finish() method,once i presses the back button in MainActivity the application is available in the application tray (where you can find list of runnning / minimized apps) once I click my application it is launching the Details Activity.
    – Jegan
    Nov 11 at 6:30










  • For the second issue I have a fragment on top of MainActivity. That fragment is holding the recycler view
    – Jegan
    Nov 11 at 6:33
















1














I am developing one application, which will show a list of Card View inside a Recycler view in MainActivity. While clicking the Card view it will launch different activity. The flow is
SplashScreenActivity -> MainActivity(Recycler View) -> DetailsActivity
The flow is working fine if we launch the application by clicking app icon.



As part of this app, I am sending FCM notifications to the app. Whenever notification is received DetailsActivity will be launched, during that time MainActivity will not be in the stack. I am staring the MainActivity whenever backbutton of Details Activity is being pressed.If DetailsActivity is not called from Notification then I am not calling MainActivity again.



Below is the code snippet I have done to handle back button in Details Activity.



@Override
public void onBackPressed() {
if (isFromNotification) {
System.out.println("Start from Notification");
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
super.onBackPressed();
startActivity(intent);
}else {
System.out.println("Start from Normal Mode");
super.onBackPressed();
}
}


I have two issues here.



i) When FCM is received Notification is viewed in DetailsActivity. After pressing the back button MainActivity is launched and DetailsActivity is not killed. After Pressing the back button from MaainActivity again Details Activity is launched.



ii) Whenever MainActivity is launched by after receiving notification (By Clicking back button of Details Activity), recyclerview elements are not clickable.



I tried my best to explain the issue, your help is really appreciated !!!










share|improve this question






















  • for first issue you can just add a finish(); after starting the mainactivity in your if condition. It will kill the detailsActivity on backpressed. For second issue, we might need more details on code or xml file to look into.
    – ManishPrajapati
    Nov 11 at 6:21












  • added finish() method,once i presses the back button in MainActivity the application is available in the application tray (where you can find list of runnning / minimized apps) once I click my application it is launching the Details Activity.
    – Jegan
    Nov 11 at 6:30










  • For the second issue I have a fragment on top of MainActivity. That fragment is holding the recycler view
    – Jegan
    Nov 11 at 6:33














1












1








1







I am developing one application, which will show a list of Card View inside a Recycler view in MainActivity. While clicking the Card view it will launch different activity. The flow is
SplashScreenActivity -> MainActivity(Recycler View) -> DetailsActivity
The flow is working fine if we launch the application by clicking app icon.



As part of this app, I am sending FCM notifications to the app. Whenever notification is received DetailsActivity will be launched, during that time MainActivity will not be in the stack. I am staring the MainActivity whenever backbutton of Details Activity is being pressed.If DetailsActivity is not called from Notification then I am not calling MainActivity again.



Below is the code snippet I have done to handle back button in Details Activity.



@Override
public void onBackPressed() {
if (isFromNotification) {
System.out.println("Start from Notification");
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
super.onBackPressed();
startActivity(intent);
}else {
System.out.println("Start from Normal Mode");
super.onBackPressed();
}
}


I have two issues here.



i) When FCM is received Notification is viewed in DetailsActivity. After pressing the back button MainActivity is launched and DetailsActivity is not killed. After Pressing the back button from MaainActivity again Details Activity is launched.



ii) Whenever MainActivity is launched by after receiving notification (By Clicking back button of Details Activity), recyclerview elements are not clickable.



I tried my best to explain the issue, your help is really appreciated !!!










share|improve this question













I am developing one application, which will show a list of Card View inside a Recycler view in MainActivity. While clicking the Card view it will launch different activity. The flow is
SplashScreenActivity -> MainActivity(Recycler View) -> DetailsActivity
The flow is working fine if we launch the application by clicking app icon.



As part of this app, I am sending FCM notifications to the app. Whenever notification is received DetailsActivity will be launched, during that time MainActivity will not be in the stack. I am staring the MainActivity whenever backbutton of Details Activity is being pressed.If DetailsActivity is not called from Notification then I am not calling MainActivity again.



Below is the code snippet I have done to handle back button in Details Activity.



@Override
public void onBackPressed() {
if (isFromNotification) {
System.out.println("Start from Notification");
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
super.onBackPressed();
startActivity(intent);
}else {
System.out.println("Start from Normal Mode");
super.onBackPressed();
}
}


I have two issues here.



i) When FCM is received Notification is viewed in DetailsActivity. After pressing the back button MainActivity is launched and DetailsActivity is not killed. After Pressing the back button from MaainActivity again Details Activity is launched.



ii) Whenever MainActivity is launched by after receiving notification (By Clicking back button of Details Activity), recyclerview elements are not clickable.



I tried my best to explain the issue, your help is really appreciated !!!







android






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 6:15









Jegan

83




83












  • for first issue you can just add a finish(); after starting the mainactivity in your if condition. It will kill the detailsActivity on backpressed. For second issue, we might need more details on code or xml file to look into.
    – ManishPrajapati
    Nov 11 at 6:21












  • added finish() method,once i presses the back button in MainActivity the application is available in the application tray (where you can find list of runnning / minimized apps) once I click my application it is launching the Details Activity.
    – Jegan
    Nov 11 at 6:30










  • For the second issue I have a fragment on top of MainActivity. That fragment is holding the recycler view
    – Jegan
    Nov 11 at 6:33


















  • for first issue you can just add a finish(); after starting the mainactivity in your if condition. It will kill the detailsActivity on backpressed. For second issue, we might need more details on code or xml file to look into.
    – ManishPrajapati
    Nov 11 at 6:21












  • added finish() method,once i presses the back button in MainActivity the application is available in the application tray (where you can find list of runnning / minimized apps) once I click my application it is launching the Details Activity.
    – Jegan
    Nov 11 at 6:30










  • For the second issue I have a fragment on top of MainActivity. That fragment is holding the recycler view
    – Jegan
    Nov 11 at 6:33
















for first issue you can just add a finish(); after starting the mainactivity in your if condition. It will kill the detailsActivity on backpressed. For second issue, we might need more details on code or xml file to look into.
– ManishPrajapati
Nov 11 at 6:21






for first issue you can just add a finish(); after starting the mainactivity in your if condition. It will kill the detailsActivity on backpressed. For second issue, we might need more details on code or xml file to look into.
– ManishPrajapati
Nov 11 at 6:21














added finish() method,once i presses the back button in MainActivity the application is available in the application tray (where you can find list of runnning / minimized apps) once I click my application it is launching the Details Activity.
– Jegan
Nov 11 at 6:30




added finish() method,once i presses the back button in MainActivity the application is available in the application tray (where you can find list of runnning / minimized apps) once I click my application it is launching the Details Activity.
– Jegan
Nov 11 at 6:30












For the second issue I have a fragment on top of MainActivity. That fragment is holding the recycler view
– Jegan
Nov 11 at 6:33




For the second issue I have a fragment on top of MainActivity. That fragment is holding the recycler view
– Jegan
Nov 11 at 6:33












1 Answer
1






active

oldest

votes


















0














The PendingIntent you include in the notification shown by your FCM message can describe a backstack with your MainActivity below your DetailsActivity.



TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(new Intent(context, MainActivity.class));
stackBuilder.addNextIntent(new Intent(context, DetailsActivity.class));
PendingIntent notificationIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);





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%2f53246333%2fandroid-open-mainactivity-after-click-back-button-in-notification-activity%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














    The PendingIntent you include in the notification shown by your FCM message can describe a backstack with your MainActivity below your DetailsActivity.



    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addNextIntent(new Intent(context, MainActivity.class));
    stackBuilder.addNextIntent(new Intent(context, DetailsActivity.class));
    PendingIntent notificationIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);





    share|improve this answer


























      0














      The PendingIntent you include in the notification shown by your FCM message can describe a backstack with your MainActivity below your DetailsActivity.



      TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
      stackBuilder.addNextIntent(new Intent(context, MainActivity.class));
      stackBuilder.addNextIntent(new Intent(context, DetailsActivity.class));
      PendingIntent notificationIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);





      share|improve this answer
























        0












        0








        0






        The PendingIntent you include in the notification shown by your FCM message can describe a backstack with your MainActivity below your DetailsActivity.



        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addNextIntent(new Intent(context, MainActivity.class));
        stackBuilder.addNextIntent(new Intent(context, DetailsActivity.class));
        PendingIntent notificationIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);





        share|improve this answer












        The PendingIntent you include in the notification shown by your FCM message can describe a backstack with your MainActivity below your DetailsActivity.



        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addNextIntent(new Intent(context, MainActivity.class));
        stackBuilder.addNextIntent(new Intent(context, DetailsActivity.class));
        PendingIntent notificationIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 7:44









        krage

        312




        312






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53246333%2fandroid-open-mainactivity-after-click-back-button-in-notification-activity%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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini