How to return back to a fragment in viewpager from an Activity's fragment

android-viewpager change page from fragment
viewpager with activity android example
viewpager with fragments example in android
android viewpager prev/next button example
fragmentstatepageradapter
implement viewpager in fragment android
viewpager inside fragment
android-viewpager with one fragment

I have a viewpager with 2 fragments(A and B) in MainActivity. In the fragments in viewpager, I have a recyclerView for each fragment that contains a list of items. When I click on an item of a fragment viewpager, it displays a new Activity with a new fragment (C) on it. But when I press the back button on toolbar, it always went back to the MainActivity with fragment A of viewpager, even if I click on item of fragment B.

In androidManifest.xml, I added "parentActivity: MainActivity" to the new Activity. If I don't set that, the back button would not response. I need help to go back to the correct fragment when I press back button.

I appreciate all answers.


Very easy. Keep an index variable in your activity in which viewpager and fragments are loaded. When you switch to another activity, do it via this activities method and update that variable with the index of the current selected page/fragment in viewpager by

getCurrentItem

This method will return the index which you can store then there are two options for you: If you know that your activity is going to be resumed from that activity only then you can write below method in onResume:

setCurrentItem

Or else you can go for startActivityForResult and achieve the same. Let me know if any queries.

ViewPager in blank when I return back to the fragment, MainActivity. Only one activity to support all fragments in the app. . case ConstantsMenu� Problem is that :: Suppose I navigate as follows >> Fragment_1 > Fragment_2 > Fragment_3 , and then from navigation drawer I select again Fragment_1 and navigate same as below and later on press back key, I find following sequence Fragment_1 < Fragment_2 < Fragment_3 < Fragment_1 < Fragment_2 < Fragment_3 – Hvn Aug 17 '14 at 16:29


First of all let assume you pressed something to open C fragment from B fragment. Here you need to pass some value to inform that C was opened from B fragment.

Send data from fragment to activity

Intent intent = new Intent(getActivity().getBaseContext(),
                    TargetActivity.class);
            intent.putExtra("B", message);
            getActivity().startActivity(intent);

Here value "B" means that activity with fragment C is opened by fragment B. For A fragment you need to change it to "A" or whatever you want.

Receive data in activity and send it back if Back button pressed

    Intent intent = getIntent();
    String message = intent.getStringExtra("message");
    back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SecondActivity.class, FirstActivity.class)
                intent.putString(message)
            }
        });

Receive data in FirstActivity and set necessary page

Intent intent = getIntent();
String message = intent.getStringExtra("message");
if(message == "B"){
    viewPager.setCurrentItem(1) //1 is index of your second page 
}else {
    viewPager.setCurrentItem(0)
}

Try this code and let me know if something goes wrong

ViewPager with FragmentPagerAdapter, A ViewPager is a layout which can be added to any layout XML file inside a root If the Activity needs to be able listen for changes to the page selected or other getCurrentItem()); // returns current Fragment item displayed within the pager. I have a fragment AroundMeFragment which has a view pager with two fragments MapFragment and ListFragment.When I enter the first time in this fragment all is ok, but when I click an item to enter to a detail view in a new fragment, and return back to AroundMeFragment, the view is blank and no item is draw.


First, for going to "NewActivity" just start the activity using intent and not finish the "MainActivity".

And For coming back to "MainActivity", just finish the "NewActivity"... hopefully, this will work!

Android Fragments Common Queries & Common Mistakes, Fragment should be used within the Activity. A greatest Fragments when using ViewPager and when to use Back Navigation. People always come across the issue remember ViewPager fragments are managed by� in this code ‘R.id.viewpager’ is real id of your ViewPager. and ‘viewPager.getCurrentItem()’ method will return currently selected fragment in ViewPager. You can also provide specific position of fragment if you want to access only 1 fragment. for example:


Slide between fragments using ViewPager, This lesson shows you how to do screen slides with a ViewPager Create a Fragment class that returns the layout that you just created in the� Next, we need to tune these properties of the pager in the containing fragment or activity: ViewPager vpPager = (ViewPager) view. findViewById (R. id. vpPager); vpPager. setClipToPadding (false); vpPager. setPageMargin (12); // Now setup the adapter as normal. Finally we need to adjust the width inside the adapter:


Android Fragments: Common Queries & Mistakes, An activity can contain any number of fragments. Whenever you are ViewPager inside a fragment you will use You should ensure that pressing the Back button on a detail screen returns the user to the master screen. If we open the fragment, push a task, and then quickly press back to return to a previous activity, when the task is finished, it will try to access the activity in onPostExecute() by calling the getActivity() method.


How to refresh data in ViewPager Fragment ?, I have ViewPager in my Activity which hosts 6 Fragment. OnPageChangeListener is the correct way to go, but you will need to refactor your� ViewPager is part of AndroidX. For more information, see Using AndroidX. Note: For sliding screens, we recommend the improved ViewPager2 library. For more information, see Slide between fragments using ViewPager2 and the ViewPager2 migration guide.