Ho un'attività che inizialmente ospita un ViewPager, collegato a un FragmentPagerAdapter.Sostituire ViewPager con frammento - Quindi spostarsi indietro
Quando l'utente fa clic su un elemento all'interno del frammento figlio di ViewPager, sto utilizzando FragmentTransaction per sostituire una vista contenitore vuota con un nuovo frammento a cui voglio navigare.
Se si utilizza addToBackStack() sulla transazione, eseguire il commit della transazione e quindi tornare indietro, non si ritorna alle visualizzazioni di ViewPager (il layout iniziale).
Se non si utilizza addToBackStack() sulla transazione, eseguire il commit della transazione e quindi tornare indietro, l'applicazione si chiude.
Appare evidente che ViewPager non viene aggiunto al backstack (il che non è sorprendente dato che non è un frammento in sé). Ma mi aspetto che il comportamento predefinito sia che la stampa posteriore mi riporta indietro a quella attività iniziale Visualizza (il ViewPager).
Sulla base di ciò che ho letto, sembra che forse a causa di una transazione frammentata, ViewPager o PagerAdapter perdano la traccia di quale frammento dovrebbe essere visualizzato.
Sono davvero confuso con questo, ma ho finito per creare un enorme pasticcio di codice che sovrascrive il onBackPress e mostra e nasconde le visualizzazioni viewpager. Avrei pensato che esiste un modo più semplice per utilizzare i comportamenti predefiniti per eseguire la navigazione appropriata.
tl; dr
A è un Viewpager frammenti hosting. B è un nuovo frammento.
Quando si sostituisce A con B, e quindi si preme indietro, mi aspetto di tornare ad A, ma ciò non sta accadendo.
Qualsiasi consiglio sarebbe molto apprezzato.
Codice:
MainActivity:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
headingLayout = (RelativeLayout) findViewById(R.id.headingLayout);
headingLayout.setVisibility(View.GONE);
// Set up the ViewPager, attaching the adapter and setting up a listener
// for when the
// user swipes between sections.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setPageMargin(8);
/** Getting fragment manager */
FragmentManager fm = getSupportFragmentManager();
/** Instantiating FragmentPagerAdapter */
MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);
/** Setting the pagerAdapter to the pager object */
mViewPager.setAdapter(pagerAdapter);
.
.
.
}
public void onListItemClicked(Fragment fragment) {
fromPlayer = false;
InitiateTransaction(fragment, true);
}
public void InitiateTransaction(Fragment fragment, boolean addToBackStack) {
invalidateOptionsMenu();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragmentContainer, fragment).addToBackStack(null)
.commit();
}
PagerAdapter:
package another.music.player;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import another.music.player.fragments.AlbumListFragment;
import another.music.player.fragments.ArtistListFragment;
import another.music.player.fragments.SongListFragment;
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
/** Constructor of the class */
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
ArtistListFragment artistListFragment = new ArtistListFragment();
Bundle artistData = new Bundle();
artistData.putInt("current_page", i + 1);
artistListFragment.setArguments(artistData);
return artistListFragment;
case 1:
AlbumListFragment albumListFragment = new AlbumListFragment();
Bundle albumData = new Bundle();
albumData.putInt("current_page", i + 1);
albumData.putBoolean("showHeader", false);
albumListFragment.setArguments(albumData);
return albumListFragment;
default:
SongListFragment songListFragment = new SongListFragment();
Bundle songData = new Bundle();
songData.putInt("current_page", i + 1);
songListFragment.setArguments(songData);
return songListFragment;
}
}
/** Returns the number of pages */
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Artists";
case 1:
return "Albums";
default:
return "Songs";
}
}
}
xml principale (contenente fragmentContainer & ViewPager):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/app_background_ics" >
<RelativeLayout
android:id="@+id/headingLayout"
android:layout_width="match_parent"
android:layout_height="56dp" >
</RelativeLayout>
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/headingLayout" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<android.support.v4.view.PagerTabStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
</RelativeLayout>
SI! Grazie. –