gpt4 book ai didi

android - 如果不覆盖 android 中的 onBackPressed() 方法,addToBackStack() 方法将不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 12:33:05 24 4
gpt4 key购买 nike

这是我编写的 MainActivity 的代码。我正在第一个屏幕中加载 fragment 列表。当用户点击任何列表项时,行星名称将在我在单独的类中定义的详细信息 fragment 中显示给用户。

我正在将“Fragment Planet List”->“Fragment Planet Detail”事务添加到后台堆栈。所以我期望的是,当我从行星细节 fragment 中按下后退按钮时,它应该会在手机中加载行星列表。但事实并非如此。

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.meditab.fragments.fragment.FragmentPlanetDetail;
import com.meditab.fragments.fragment.FragmentPlanetList;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends ActionBarActivity implements Callback {

private FrameLayout frmPlanetList;
private FrameLayout frmPlanetDetail;
private boolean isPhone;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

frmPlanetList = (FrameLayout) findViewById(R.id.frmPlanetList);
frmPlanetDetail = (FrameLayout) findViewById(R.id.frmPlanetDetail);

if (null != frmPlanetDetail) {
isPhone = false;
} else {
isPhone = true;
}

FragmentManager fragmentManager = getFragmentManager();

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

fragmentTransaction.replace(R.id.frmPlanetList, new FragmentPlanetList());

fragmentTransaction.commit();

}


@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_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
public void onListItemClicked(int intPosition) {

FragmentManager fragmentManager = getFragmentManager();

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

List<String> lstPlanetArray = getPlanetArray();
String strPlanetName = lstPlanetArray.get(intPosition);

FragmentPlanetDetail fragmentPlanetDetail = FragmentPlanetDetail.newInstance(strPlanetName);
if (isPhone) {
fragmentTransaction.addToBackStack(null);
fragmentTransaction.replace(R.id.frmPlanetList, fragmentPlanetDetail);
} else {
fragmentTransaction.replace(R.id.frmPlanetDetail, fragmentPlanetDetail);
}

fragmentTransaction.commit();

}

private List<String> getPlanetArray() {
List<String> lstPlanets = new ArrayList<>(10);
lstPlanets.add("Mercury");
lstPlanets.add("Venus");
lstPlanets.add("Earth");
lstPlanets.add("Mars");
lstPlanets.add("Jupiter");
lstPlanets.add("Saturn");
lstPlanets.add("Uranus");
lstPlanets.add("Neptune");
lstPlanets.add("Saturn");

return lstPlanets;
}

}

但是,如果我重写 backPress 方法并以编程方式弹出返回堆栈,它就可以正常工作。

@Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() != 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}

如果我想实现这种行为,是否需要以这种方式覆盖 onBackPressed() 方法?没有记录您需要在此链接中覆盖此方法。 Android Back Press Fragment Documentation

fragment 行星细节类

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.meditab.fragments.R;

/**
* Created by BHARGAV on 25-Dec-14.
*/
public class FragmentPlanetDetail extends Fragment {

private String strPlanetName;

public FragmentPlanetDetail() {

}

public static FragmentPlanetDetail newInstance(String strPlanetName) {
FragmentPlanetDetail fragmentPlanetDetail = new FragmentPlanetDetail();

Bundle bundle = new Bundle();
bundle.putString("Planet_Name", strPlanetName);
fragmentPlanetDetail.setArguments(bundle);
return fragmentPlanetDetail;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Bundle bundle = getArguments();
if (null != bundle) {
strPlanetName = bundle.getString("Planet_Name");
}
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.planet_name, container, false);

TextView txtPlanetName = (TextView) rootView.findViewById(R.id.txtPlanetName);
txtPlanetName.setText(strPlanetName);

return rootView;
}
}

fragment 行星列表类:

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.meditab.fragments.Callback;
import com.meditab.fragments.R;

import java.util.ArrayList;
import java.util.List;

/**
* Created by BHARGAV on 25-Dec-14.
*/
public class FragmentPlanetList extends Fragment {

private ListView listView;
private ArrayAdapter<String> stringArrayAdapter;

private Callback callback;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.listview, container, false);

listView = (ListView) rootView.findViewById(R.id.listView);
stringArrayAdapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1, getPlanetArray());
listView.setAdapter(stringArrayAdapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
callback.onListItemClicked(position);
}
});
return rootView;
}



@Override
public void onAttach(Activity activity) {

super.onAttach(activity);

callback = (Callback) activity;
}

private List<String> getPlanetArray() {
List<String> lstPlanets = new ArrayList<>(5);
lstPlanets.add("Mercury");
lstPlanets.add("Venus");
lstPlanets.add("Earth");
lstPlanets.add("Mars");
lstPlanets.add("Jupiter");
lstPlanets.add("Saturn");
lstPlanets.add("Uranus");
lstPlanets.add("Neptune");
lstPlanets.add("Saturn");

return lstPlanets;
}

}

回调接口(interface):

/**
* Created by BHARGAV on 26-Dec-14.
*/
public interface Callback {

public void onListItemClicked(int intPosition);
}

谢谢。如果需要任何其他详细信息或代码,请告诉我。

最佳答案

好的。所以它不起作用的原因是 ActionBarActivty 和 Activity 类差异的冲突。以及FragmentTransactiongetSupportFragmentManager()getFragmentManager()方法的区别。

Action 条 Activity :

我使用的是 android.support.v7.app.ActionBarActivity 的 ActionBarActivity。这意味着我使用的是 v7 兼容库

fragment 管理器: fragment 管理器不是来自兼容库。直接是

import android.app.FragmentManager;
import android.app.FragmentTransaction;

如您在 MainActivity 类中所见。

fragment :android.app.Fragment;是在单独的 fragment 类中导入的类。

因此,一旦我从 ActionBarActivity 更改为 Activity 类,一切正常。当我更改 FragmentManager、FragmentTransaction 和 Fragment 类以支持库类时,情况也是如此。

所以在任何修改之后,事情开始正常工作。谢谢。

关于android - 如果不覆盖 android 中的 onBackPressed() 方法,addToBackStack() 方法将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27659038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com