ListFragment是回去后空 [英] ListFragment is empty after going back

查看:257
本文介绍了ListFragment是回去后空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经与HoloEverywhere一个片段容器一个简单的活动。 ListFragment出现那里开始,通常出现。然后,点击列表项替换ListFragment与另一个片段,并增加了它backstack。但是,当我pressing后退按钮,列表是空的,虽然它恢复正常。这里也有一些code:

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:

活动

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();
    }

}

}

断肢布局:

<?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);
}

}

我已经测试了2.3.3和4.0.4,结果是一样的。那么,如何使其正常工作?

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

推荐答案

当您更换您的片段,我认为onDestroyView()方法被调用。
您可以使用transaction.hide(本),而不是:

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();

或设置列表适配器在onResume()方法,当你的片段回前台,onResume将被调用。

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

这篇关于ListFragment是回去后空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆