ListFragment is empty after going back

你离开我真会死。 提交于 2019-12-13 04:21:52

问题


I have a simple activity built with HoloEverywhere with one fragment container. ListFragment appears there on start, and appears normally. Then, click on list item replaces ListFragment with another fragment and adds it to backstack. But when I'm pressing back button, list is empty, although it resumes normally. There's some code:

Activity

public class MainActivity extends Activity implements SongsFragment.IActivityCallback {

public final static String TAG = "Lyrics";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(TAG, "STarted.");
    setContentView(R.layout.frags);

}

@Override
public void itemSelected(int id) {
    Log.d(TAG, "itemSelected");
    LyricsFragment lyrics = new LyricsFragment();
    if(findViewById(R.id.frag_frame) != null) {
        Bundle args = new Bundle();
        args.putInt("id", id);

        lyrics.setArguments(args);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frag_frame, lyrics);
        transaction.addToBackStack(null);
        transaction.commit();
    }

}

}

frags layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frag_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<fragment
    android:id="@+id/fragment1"
    android:name="android.app.ListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.anth.lyrics.SongsFragment"
    tools:layout="@layout/song_fragment" />
</FrameLayout>

LyricsFragment:

public class LyricsFragment extends Fragment {
...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    this.container = container;
    View ret = inflater.inflate(R.layout.lyricsview, container, false);
    return ret;
}

...

SongsFragment:

public class SongsFragment extends ListFragment {
private IActivityCallback callback;
private ArrayList<Song> songs;
final static String TAG = "SOng Fragment";

public interface IActivityCallback {
    public void itemSelected(int id);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    TextView t_id = (TextView) v.findViewById(R.id.song_id);
    int s_id = Integer.valueOf( t_id.getText().toString() );
    Log.d(TAG, "Select item");
    callback.itemSelected(s_id);
    super.onListItemClick(l, v, position, id);
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    callback = (IActivityCallback) getActivity();
    DBRead db = new DBRead(getActivity());
    songs = db.getLast(10);
    db.close();
    SongListAdapter adapt = new SongListAdapter(getActivity(), R.layout.songs_item, songs);
    setListAdapter(adapt);
}

}

I've tested it on 2.3.3 and on 4.0.4, result was the same. So, how to make it work properly?


回答1:


When you replace your fragment, I think that the onDestroyView() method is called. You can use transaction.hide(this) instead :

        ft.addToBackStack(null);
        ft.hide(this);
        ft.add(R.id.frag_frame, lyrics);
        ft.commit();

or set your list adapter in the onResume() method, when your fragment is back to foreground, the onResume will be called.




回答2:


Implementing onsaveInstanceSate() method may resole your issue?



来源:https://stackoverflow.com/questions/16359035/listfragment-is-empty-after-going-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!