我正在开发一个带片段的应用程序。我的问题是,当我按下后退按钮,从任何屏幕,应用程序关闭。应用程序显示预期的布局,但立即关闭。这是我片段的代码,其中包含一个Button。
public class LoginFragment extends android.support.v4.app.Fragment implements View.OnClickListener, OnBackPressed{
private StatusFragment.StatusListener statusListener;
public static LoginFragment newInstance() {return new LoginFragment();}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_login, container, false); //este fragment_profile se muestra
Button go_login=(Button)layout.findViewById(R.id.button_gologin);
go_login.setOnClickListener(this);
Button go_registration=(Button)layout.findViewById(R.id.button_goregistration);
go_registration.setOnClickListener(this);
return layout;
}
@Override
public void onBackPressed() {
//on Back Pressed
}
@Override
public void onDetach() {
super.onDetach();
statusListener = null;
}
@Override
public void onClick(View v)
{
switch (v.getId()){
case R.id.button_gologin:{
Model model=(Model)getActivity().getApplicationContext();
EditText text_log = (EditText)getActivity().findViewById(R.id.editText_user);
EditText text_pass = (EditText)getActivity().findViewById(R.id.editText_pass);
model.sendLogin(text_log.getText().toString(),text_pass.getText().toString());
model.isPasswordCorrect();
break;
}
然后我们有了MainActivity
public class MainActivity extends FragmentActivity implements AccountFragment.OnFragmentInteractionListener,
StatusFragment.StatusListener, ObserverModel {
}
private void showLogin() {
LoginFragment fragment = (LoginFragment) fragmentManager.findFragmentByTag(LOGIN_FRAGMENT);
if (fragment == null) {
Log.d(TAG, "Открываем статус");
} else {
Log.d(TAG, "Статус открыт");
}
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, loginFragment, LOGIN_FRAGMENT).addToBackStack(STATUS);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
transaction.addToBackStack(null);
transaction.commit();
}
@Override
public void onBackPressed() {
Fragment currentFragment = getSupportFragmentManager().getFragments().get(getSupportFragmentManager().getBackStackEntryCount() - 1);
if (currentFragment instanceof OnBackPressed) {
((OnBackPressed) currentFragment).onBackPressed();
} else {
super.onBackPressed();
}
}
问题出在哪里?我正在阅读其他人发布类似物,但无法找到解决方案。感谢
答案 0 :(得分:2)
在您的活动中,请使用onBackPressed
方法:
@Override
public void onBackPressed() {
if (fragment != null && fragment.getChildFragmentManager().getBackStackEntryCount() > 0){
fragment.getChildFragmentManager().popBackStack();
}else {
super.onBackPressed();
}
}
添加
在showLogin
方法中:
Fragment fragment = new LoginFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container,fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
答案 1 :(得分:0)
将以下方法放在LoginFragment类中:
public boolean onBackPressed() {
return false;
}
将以下方法放在MainActivity类中:
@Override
public void onBackPressed() {
boolean eventConsumed = false;
List<Fragment> fragments = getSupportFragmentManager().getFragments();
if (fragments != null) {
for (Fragment fragment : fragments) {
if (fragment instanceof BaseFragment) {
eventConsumed = eventConsumed
|| ((BaseFragment) fragment).onBackPressed();
}
}
}
if (!eventConsumed) {
super.onBackPressed();
}
}
试试这段代码。
答案 2 :(得分:0)
试试这段代码:
file_lists
答案 3 :(得分:0)
使用这种方式,创建一个接口。
public interface IOnBackPressed {
boolean onBackPressed();
}
在FragmentBackHelper类上创建
import android.support.v4.app.Fragment;
public class FragmentBackHelper extends Fragment implements IOnBackPressed {
@Override
public boolean onBackPressed() {
return true;
}
}
然后从Fragment Class中调用此方法。
@Override
public boolean onBackPressed() {
if (fragment != null && fragment.getChildFragmentManager().getBackStackEntryCount() > 0){
fragment.getChildFragmentManager().popBackStack())
return super.onBackPressed();
}