【问题标题】:ListFragment is empty after going back返回后ListFragment为空
【发布时间】:2013-05-03 12:32:51
【问题描述】:

我有一个使用 HoloEverywhere 构建的简单活动,带有一个片段容器。 ListFragment 在开始时出现在那里,并且正常显示。然后,单击列表项将 ListFragment 替换为另一个片段并将其添加到 backstack。但是当我按下后退按钮时,列表是空的,尽管它会正常恢复。有一些代码:

活动

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>

歌词片段:

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 上测试过,结果是一样的。那么,如何让它正常工作呢?

【问题讨论】:

    标签: android android-fragments android-listfragment android-holo-everywhere


    【解决方案1】:

    当你替换你的片段时,我认为调用了 onDestroyView() 方法。 您可以使用 transaction.hide(this) 代替:

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

    或者在 onResume() 方法中设置你的列表适配器,当你的片段回到前台时,会调用 onResume。

    【讨论】:

    • onResume() 在我从替换到的新片段中返回时不会触发。所以@nbe_42 解决方案似乎更好
    【解决方案2】:

    实现 onsaveInstanceSate() 方法可能会解决您的问题?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多