1

I have three fragments in the app. I am creating, call them f1,f2, f3. All these are contained in the MainActivity. In the third fragment f3, I have a button which takes a user to an activity where they can post a picture. Now whenever the post is successful (using onSuccessListener), I am displaying a toast message and thereafter taking the user to the MainActivity using intent.

Everything works fine, but I wanted to take the user to the previous fragment f3, rather than the MainActivity which launches with fragment f1. How can I achieve this?

Here is what I've tried so far:

@Override
public void onBackPressed(){
    finish();
}

// didnt work

Here is the current code that am using

//my other code

PostsRef.child(current_user_id + timesfm).updateChildren(postsMap)
                        .addOnCompleteListener(new OnCompleteListener() {
                            @Override
                            public void onComplete(@NonNull Task task)
                            {
                                if(task.isSuccessful())
                                {
                                    Toast.makeText(PostActivity.this, "Post updated.", Toast.LENGTH_SHORT).show();
                                    loadingBar.dismiss();
                                    SendUserToMain();
                                }
                                else
                                {
                                    Toast.makeText(PostActivity.this, "Error Occured.Please try again.", Toast.LENGTH_SHORT).show();
                                    loadingBar.dismiss();
                                }
                            }
                        });
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

private void SendUserToMain() {
    Intent mIntent = new Intent(PostActivity.this, MainActivity.class);
    mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(mIntent);
}
James Z
  • 12,209
  • 10
  • 24
  • 44
Ezra Raleigh
  • 61
  • 12
  • Since you're launching an Activity and then you're finishing it, doesn't this bring up the MainActivity with the latest fragment shown, which already is fragment f3 ? – Orestis Gartaganis Jan 12 '19 at 13:21
  • I navigate back to the MainActivity using an intent, as in the edit above. It sure works well but it doesnt bring the latest fragment, the main activity instead launches with fragment f1. – Ezra Raleigh Jan 12 '19 at 13:35
  • Yeah, I see your error. You're launching a new instance of MainActivity, hence it starts as it should by default, with the fragment f1. Instead, finish your activity to return to the originating MainActivity with your fragment f3 displayed. Check my answer below for a more detailed explanation. – Orestis Gartaganis Jan 12 '19 at 14:58

5 Answers5

0

Instead of using startActivity(intent) from your fragment use startActivityForResult(intent,requsetCode) in our fragment and use setReult(requestCode) and finish() methods in your target activity.

Abilash
  • 313
  • 5
  • 15
0

Code Credit: Nishant

You can use startActivityForResult(). From your MainActivity start PostActivity and don't finish MainActivity:

Intent intent = new Intent(this, PostActivity.class);
startActivityForResult(intent , 10);
// don't finish..

Now when you are done in your PostActivity go to MainActivity this way:

private void SendUserToMain() {
    Intent intent = new Intent();
    intent.putExtra("frag",3);
    setResult(Activity.RESULT_OK,intent);
    finish();
}

Now in your MainActivity:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 10) {
        if(resultCode == Activity.RESULT_OK){
            int fragment = data.getIntExtra("frag");
            if (fragment == 3){
                // go to f3
            }
        }
    }
}
Abubakar Azeem
  • 314
  • 1
  • 4
  • 14
0

All you need to do instead of calling SendUserToMain(); is call finish() as the previous Activity with the last fragment state is on the backstack. At the moment in your SendUserToMain(); method is creating a new instance of MainActivity which will call its onCreate() method and set up the Activity as if it were new (which it is).

Ivan Wooll
  • 4,145
  • 3
  • 23
  • 34
0

Your problem is that instead of finish() in your PostActivity, in order to return to the original MainActivity instance, you launch a new instance with your method

private void SendUserToMain() {
    Intent mIntent = new Intent(PostActivity.this, MainActivity.class);
    mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(mIntent);
}

Instead of doing that, just do:

private void SendUserToMain() {
    finish();
}

in order to finish the PostActivity and return to your original MainActivity instance. Of course, this means that you'll also NOT finish your MainActivity when you're first going to the PostActivity()!

// Sample method of you launching PostActivity, I don't know your actual implementation
void sendUserToPostActivity() {
    Intent mIntent = new Intent(MainActivity.this, PostActivity.class);
    startActivity(mIntent);
    // finish() <-- Don't do that, so your MainActivity remains in the backstack
}

If you want something more sophisticated, like adding a result to your return, you can also do the startActivityForResult -> setResult routine as described by other people.

  • You Sir, deserve a beer !! You have no idea for how long I've been struggling with this issue. Thanks a bunch!! Really. – Ezra Raleigh Jan 12 '19 at 16:44
0

Thanks a lot for your help guys, y'all are so kind. So with the help of @Ore, I finally figured out where the problem was. Turns out while calling PostActivity from my fragment f3, I'd also called the finish method after the intent. Here is what Id done

private void SendUserToPostActivity() {
        Intent addPostIntent = new Intent(getActivity(), NewPost.class);
        addPostIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(addPostIntent);
        getActivity().finish();
}

The correct one should be

private void SendUserToPostActivity() {
    Intent addPostIntent = new Intent(getActivity(), NewPost.class);
    startActivity(addPostIntent);
}

then to navigate back to the latest fragment, f3, I called the finish() method.

@Ore explains it better. Thanks everyone for your input though. Really appreciated!

Ezra Raleigh
  • 61
  • 12