0

我有一个使用 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;
}

...

歌曲片段:

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

4

2 回答 2

7

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

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

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

于 2013-05-03T12:46:16.690 回答
0

实施 onsaveInstanceSate() 方法可以解决您的问题吗?

于 2013-05-03T12:38:33.823 回答