我正在尝试正确处理用户按下后退按钮但没有成功。
在主activity
中按下图像后,会创建fragment
,并显示带有项目的listview
。用户现在选择其中一个项目,并执行新的query
,使用另一组项目填充listview
。现在,在选择项目后,会创建一个新的fragment
,显示用户选择的最后一项的信息。现在,我假装,如果用户按下后退按钮,则会显示之前的listview
。再按一次将显示初始listview
。我已经成功实现了第一次背压的第一部分但是进行了第二次背压我将进入主要活动,而不是显示第一个listview
。
这是用于处理片段的主activity
之后的代码
public class Fragments extends ActionBarActivity implements QueryFragment.OnQuerySelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragments);
if (findViewById(R.id.fragmentsboth) != null) {
numberOfPanes = 1;
if (savedInstanceState != null) {
return;
}
QueryFragment firstFragment = new QueryFragment();
getSupportFragmentManager().beginTransaction().add(R.id.fragmentsboth, firstFragment).commit();
} else if (savedInstanceState == null) {
numberOfPanes = 2;
QueryFragment firstFragment = new QueryFragment();
getSupportFragmentManager().beginTransaction().add(R.id.fragment_one, firstFragment).commit();
Infodata secondFragment = new Infodata();
getSupportFragmentManager().beginTransaction().add(R.id.fragment_two, secondFragment).commit();
}
}
@Override
public void onItemSelected(Bundle itemSelectedData) {
View rightLayout = findViewById(R.id.fragment_two);
if (rightLayout != null) {
if (getSupportFragmentManager().findFragmentById(R.id.fragment_two) instanceof Infodata) {
Infodata itemDataFragment = (Infodata) getSupportFragmentManager().findFragmentById(R.id.fragment_two);
itemDataFragment.populateLayoutWithData(2, itemSelectedData);
} else {
Infodata secondFragment = new Infodata();
secondFragment.setArguments(itemSelectedData);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_two, secondFragment).commit();
}
} else {
Infodata newFragment = new Infodata();
newFragment.setArguments(itemSelectedData);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragmentsboth, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
}
这是显示带有listviews
的片段的代码。
public class QueryFragment extends ListFragment {
public interface OnQuerySelectedListener {
public void onItemSelected(Bundle itemSelectedData);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
activityContext = getActivity().getApplicationContext();
if (operation == 0) {
layoutToShow = inflater.inflate(R.layout.querylist, container, false);
if (groupName == null) { //Performs the first initial query
groupQuery = true;
groupsLayout(layoutToShow, 0);
} else {
groupsLayout(layoutToShow, 1); //Comes here after the first back press
hasPerformedGroupSearch = true;
}
}
return layoutToShow;
}
private void groupsLayout(View layout, int step) {
if (step == 0) {
//Code for performing the first query
} else if (step == 1) {
//Code for performing the second query
}
cursorAdapter = new QueryAdapter(getActivity(), query, "");
listContent.setFastScrollEnabled(false);
listContent.setAdapter(cursorAdapter);
listContent.setFastScrollEnabled(true);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Bundle itemData = new Bundle();
if (groupQuery) { //Gets the data from the first query
Cursor grupo = (Cursor) getListView().getItemAtPosition(position);
groupId = grupo.getLong(0);
groupName = grupo.getString(1);
groupQuery = false;
groupsLayout(layoutToShow, 1); // Shows the data of the second query
} else {
//Puts the data of the second query in the bundle itemData
mCallback.onItemSelected(itemData);
getListView().setItemChecked(position, true);
}
}
知道如何实现我的目标吗?
答案 0 :(得分:1)
将应用的状态保存在某种静态变量中。也许是enum
。然后覆盖您的活动' onBackPressed()
并且可能会创建这样的内容:
@Override
pulic void onBackPressed () {
if (state == State.idle) {
super.onBackPressed ();
else {
// You can customize from now on...
}
}