0

I am simply trying to click back and navigate to my previous activity. My flow is this: I go to news_feed activity -> Comments activity -> User Profile activity -> click back (Go to Comments activity) -> click back (This does nothing for some reason) -> Click back (Go back to news_feed activity). I'm not sure why I have to click back twice when I try to go from Comments activity back to news_feed activity. If I go from news_feed activity -> Comments -> press back (Go to news_feed activity) this works perfectly. Here is my code:

news_feed.java:

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);
}

Comments.java:

@Override
public void onBackPressed() {
    super.onBackPressed();
    this.finish();
}

UserProfile.java:

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent intent = new Intent(UserProfile.this, Comments.class);
    Bundle bundle = new Bundle();
    bundle.putString("postId", postId);
    bundle.putString("posterUserId", posterUserId);
    bundle.putString("posterName", posterName);
    bundle.putString("postStatus", postStatus);
    bundle.putString("postTimeStamp", postTimeStamp);
    bundle.putString("postTitle", postTitle);
    intent.putExtras(bundle);
    startActivity(intent);
    finish();
}

I don't think navigating to these activities would change anything, but I can include the intents that I used to navigate to these activities also if necessary. Otherwise I just included the onBackPressed code that I had for these activities. Any ideas why this might cause a problem? Any help would be appreciated. Thanks.

user1871869
  • 3,317
  • 13
  • 56
  • 106

5 Answers5

0

You can try something like this:

private boolean clicked;
@Override
public void onBackPressed() {
    if (!clicked) { // if it was not clicked before, change the value of clicked to true and do nothing directly return, if clicked again, then it will be finish the activity.
        clicked=true;
        return;
    }
    super.onBackPressed();
}

And if you are going to use the time, then you can do like this:

private long lastClicked;
@Override
public void onBackPressed() {
    if (System.currentTimeMillis() - lastClicked  < 1000) { // within one second
        super.onBackPressed();
    }
    lastClicked = System.currentTimeMillis();
}
Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68
0

I you want to send back the result to previous activity the start activity by using

startActivityForResult() 

insead of

startActivity()

and don't override

onBackPressed()
Shubham
  • 521
  • 4
  • 11
0
public void onBackPressed() {


        if (this.lastBackPressTime < System.currentTimeMillis() - 2000) {



            this.lastBackPressTime = System.currentTimeMillis();
        } else {


            //super.onBackPressed(); for exit

            //INTENT HERE TO YOUR SECOND ACTIVITY like below

             Intent intent=new Intent(A.this,B.class);
              startActivirty(intent);
        }

}

Create a global variable

private long lastBackPressTime = 0;

Hope this will help you,Let me know..

Andolasoft Inc
  • 1,296
  • 1
  • 7
  • 16
0

In your UserProfile class, in onBackPressed() method, you are starting the Comments class again. Why do you have to do this? What is happening is, you are starting a new Comments activity onBackPressed() of the USerProfile class, so there are two instances of Comments Activity. So you feel you are pressing back btn twice.

If you have to pass data back to Comments from UserProfile class, then make use of setResult() method.

This will be of help

How to pass data from 2nd activity to 1st activity when pressed back? - android

Sending data back to the Main Activity in android

Community
  • 1
  • 1
Sahana Prabhakar
  • 581
  • 1
  • 8
  • 21
0

You don't need to override onBackPressed() if all you're going to do is call startActivity() and finish() in the current activity. The Android backstack will handle both of those for you. Moreover, in your UserProfile.java you are passing data to previous activity. For this, you should use startActvityForResult() in the Comments activity (i.e the previous activity) instead of startActivity().

To learn more about startActvityForResult() visit: https://developer.android.com/training/basics/intents/result.html

Shaishav
  • 5,282
  • 2
  • 22
  • 41