Go back to the snippet from Activity

I have three fragments in Activity C. They behave like tabs. I need to move from the fragment to the new Activity X. Now I want to return to the fragment from Activity X.

I have override onBackPressed, but I don't know how to return to the / not fragmentActivity fragment from Activity.

if I want to return to Activity for another activity, which I redefine on the pressed button, and using the deliberate call of this actvity.

I want to do something like this .. this code is for returning to previous activity

@Override public void onBackPressed() { Intent intent = new Intent(CurrentActivity.this,ActivityYouLikeToGo.class); intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(intent); } 

thanks in advance ..

+6
source share
8 answers

You can add a TAG (the string name for the fragment basically) and then load the fragment with findFragmentByTag().

The google documentation for findFragmentByTag() here .

Or you can also addToBackStack() when calling a fragment from FragmentTransaction , doc link here . from google docs:

"By calling addToBackStack (), the replacement transaction is saved to the back so that the user can cancel the transaction and return the previous fragment by clicking the" Back "button.

+3
source

First, redefine the backpressure to go to the activity in which the fragments are located: -

 @Override public void onBackPressed() { Intent intent = new Intent(CurrentActivity.this,ActivityYouLikeToGo.class); intent.putExtra("Check",1); startActivity(intent); } 

then go to the file ActivityYouLikeToGo.java and in onCreate do the following: -

 Intent intent = getIntent(); String s1 = intent.getStringExtra("Check"); if(s1.equals("1")) { s1 = ""; Fragment fragment = new YOURFRAMENTNAME(); if (fragment != null) { FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.frame_container, fragment).commit(); } } 
+4
source

The simplest way:

 @Override onBackPressed(){ finish(); } 
+4
source

if I want to return to Activity for another action I override on back pressed and using an intentional call that actvity

Usually, if you want to return, you do not need to do anything. When you click the back button of a device’s device, you will go to the previous action with the tab already open.

0
source

You can start an action that you switch to using startActivityForResult (), and then returning to the previous action, you set the result. In the action to which you return, show the fragment depending on the result. You can complete this task by hiding all the fragments that you do not want to show. This may work even if you move on to another action from a different fragment.

0
source

In the action that you started with your snippet, you can do the following:

  @Override public boolean onOptionsItemSelected(MenuItem item) { //do whatever you want here finish(); return true; } 

This works for me :)

0
source

This code works in my case. I just create an Onclick method for the button. code below: I made the onclick method clickForFriends in Button and Id is backFriendsList, and I use the android.support.v4.app.Fragment class. This method should be returned from the new action to the fragment from the Activity, and this method should be in Activity after the onCreate method

  public void clickedForFriends(View v){ if (v.getId() == R.id.backFriendsList){ onBackPressed(); } } 
0
source
 viewPager.setCurrentItem(tabPosition); 

This decision did wonders for me.

-1
source

Source: https://habr.com/ru/post/970023/


All Articles