我的片段的代碼如下所示:無法保存片段的實例屏幕旋轉後不重新啓動
public class AllIssuesFragment extends Fragment {
@Bind(R.id.list_all_issues) ListView mListAllIssues;
IssuesGetter ai;
ArrayAdapter<IssuesResponse.Issue> adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_all_issues, container, false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
setRetainInstance(true);
SharedPreferences user_data = this.getActivity().getSharedPreferences("user_data", Context.MODE_PRIVATE);
UserManager.getInstance().setUsername(user_data.getString("username", null));
UserManager.getInstance().setPassword(user_data.getString("password", null));
if(UserManager.getInstance().getUsername() == null || UserManager.getInstance().getPassword() == null) {
Intent intent = new Intent(getActivity(), ChangeUserActivity.class);
startActivity(intent);
}
else {
adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1);
if(savedInstanceState == null) {
getIssues(Constants.URI + Constants.ISSUES + Constants.OFFSET + 0);
}
mListAllIssues.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
}
@Override
public void onScroll(AbsListView absListView, int firstVisible, int visibleCount, int totalCount) {
boolean loadMore = firstVisible + visibleCount >= totalCount;
if (loadMore && ai.getStatus() == AsyncTask.Status.FINISHED) {
getIssues(Constants.URI + Constants.ISSUES + Constants.OFFSET + totalCount);
mListAllIssues.setSelection(totalCount);
}
}
});
}
}
private void getIssues(String url) {
ai = new IssuesGetter(new AsyncCallback<IssuesResponse>() {
@Override
public void onSuccess(IssuesResponse response) {
int index = mListAllIssues.getFirstVisiblePosition();
View v = mListAllIssues.getChildAt(0);
int top = (v == null)? 0 : v.getTop();
adapter.addAll(response.getIssues());
adapter.notifyDataSetChanged();
mListAllIssues.setAdapter(adapter);
mListAllIssues.setSelectionFromTop(index, top);
}
@Override
public void onError() {
Intent intent = new Intent(getActivity(), ChangeUserActivity.class);
startActivity(intent);
}
});
ai.execute(url);
}
}
MainActivity.java:
public class MainActivity extends AppCompatActivity {
@Bind(R.id.viewpager) ViewPager viewPager;
@Bind(R.id.tabs) PagerSlidingTabStrip tabStrip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
viewPager.setAdapter(new MainActivityPagerAdapter(getSupportFragmentManager()));
tabStrip.setViewPager(viewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_change_user) {
Intent intent = new Intent(this, ChangeUserActivity.class);
this.startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
我試着使用setRetainInstance(true)並檢查savedInstanceState是否爲空,但它沒有解決我的問題EM。我堅持:(
我的AsyncTask返回結果使用回撥
你能證明使用該片段活動的代碼? – Nolan
'private ArrayList cache;'then ... in'getIssues' ... if'cache'!= null然後設置適配器... if null執行asynctask並從響應中設置'cache' .getIssues()並設置適配器......關鍵是片段的保留不與UI組件一起工作...... **編輯**好像你在使用分頁......然後它不會那麼容易...... –
Selvin
@諾萊恩,我補充說。 –