私はフラグメントを持つアプリをdelevopingしています。私の問題は、任意の画面から戻るボタンを押したときに、アプリケーションが終了することです。アプリケーションは予想されるレイアウトを表示しますが、すぐに終了します。これは、ボタンを含む私の断片のコードです。onBackPressed Androidフラグメント
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();
}
}
ですか?私は他の人と同じ投稿を読んでいましたが、解決策を見つけることはできません。 MainActivityクラスのメソッドの下に入れて
public boolean onBackPressed() {
return false;
}
:LoginFragmentクラスのメソッドの下に入れおかげ
私はこの行にいくつかの問題があると思います。===> transaction.replace(R.id.container、loginFragment、LOGIN_FRAGMENT).addToBackStack(STATUS);あなたは "loginFragment"をどこで定義しましたか?私が間違っているなら私を訂正してください! – Darsh
パブリッククラスMainActivityはFragmentActivityはAccountFragment.OnFragmentInteractionListener、 StatusFragment.StatusListener、ObserverModel { LoginFragment loginFragment =新しいLoginFragment()最終的な静的文字列LOGIN_FRAGMENT = "ログイン" を実装して延びています。 – baldcl
FragmentActivityのsuper.onBackPressedをelse文に追加すると、問題は解決されます。 –