片段保存实例状态

时间:2014-02-01 10:58:47

标签: android android-fragments

我在项目点击时使用jfeinstein10 / SlidingMenu我通过setUpFragment加载片段(new Inbox()) 在Inbox片段中我有一个AsyncTask来为我的listview加载数据每次我导航到一个不同的片段,然后回到收件箱片段,异步任务被调用。

 private void setUpFragment(Fragment fragment) {
     manager = this.getSupportFragmentManager();
         ft = manager.beginTransaction();
    ft.replace(R.id.frame, fragment);

    ft.commit();

}

这是收件箱碎片类

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.e(tag, "onCreate");
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // Retrieving the currently selected item number
    Log.e(tag, "onCreateView");
    // Creating view correspoding to the fragment
    v = inflater.inflate(R.layout.fragment_layout, container, false);

    // Getting reference to the TextView of the Fragment
    TextView tv = (TextView) v.findViewById(R.id.tv_content);

    // Setting currently selected river name in the TextView
    tv.setText("Calendar");

    return v;
}

@Override
public void onStart() {
    super.onStart();
    Log.e(tag, "onStart");
    new Async().execute();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Log.e(tag, "onSaveInstance");
}

@Override
public void onPause() {
super.onPause();
Log.e(tag, "onPause");
}

@Override
public void onStop() {
    super.onStop();
    Log.e(tag, "onStop");
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    Log.e(tag, "onDestroyView");
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.e(tag, "onDestroy");
}

@Override
public void onDetach() {
    super.onDetach();
    Log.e(tag, "onDetach");
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    Log.e(tag, "Attach");
}

class Async extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        Log.e(tag, "onPostExecute");
    }
}

调用setupFragment

的代码
private Fragment streamFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_expert_main);
    setUpActionBar();
    initFragments();
    setUpFragment(streamFragment);

    setSlidingMenu();

}

private void initFragments() {
    streamFragment = new MyStreamFragment();
    calenderFragment = new MyCalendarFragment();

}


menu1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            menuPressed = 1;

            if(streamFragment == null)
                Log.e("stream fragment" , " null");

            setUpFragment(streamFragment);
            currentFragment = streamFragment;
            setTextViewBackground();
            menu_sliding.toggle();

        }
    });

1 个答案:

答案 0 :(得分:0)

放入“new Async()。execute();”在onCreate()而不是onStart()。首次创建片段时调用onCreate,而每次片段恢复时调用onStart。检查片段lifecycle