2017-01-16 18 views
0

私はフラグメントを持つアプリを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クラスのメソッドの下に入れおかげ

+0

私はこの行にいくつかの問題があると思います。===> transaction.replace(R.id.container、loginFragment、LOGIN_FRAGMENT).addToBackStack(STATUS);あなたは "loginFragment"をどこで定義しましたか?私が間違っているなら私を訂正してください! – Darsh

+0

パブリッククラスMainActivityはFragmentActivityはAccountFragment.OnFragmentInteractionListener、 StatusFragment.StatusListener、ObserverModel { LoginFragment loginFragment =新しいLoginFragment()最終的な静的文字列LOGIN_FRAGMENT = "ログイン" を実装して延びています。 – baldcl

+0

FragmentActivityのsuper.onBackPressedをelse文に追加すると、問題は解決されます。 –

答えて

0

@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(); 
    } 
} 

は、このコードを試してみてください。あなたの活動に

+0

画面上のフラグメントから「戻るボタン」を押した後にアプリケーションが落ち続ける – baldcl

+0

このコードを試してください。 @Override パブリックvoid onBackPressed(){ super.onBackPressed(); fragment.backButtonWasPressed(); } –

+0

私の編集した回答を見て、これを試してみてください。 –

2

onBackPressed方法でこれを使用する:あなたのshowLogin方法で

を追加

@Override 
public void onBackPressed() { 
    if (fragment != null && fragment.getChildFragmentManager().getBackStackEntryCount() > 0){ 
     fragment.getChildFragmentManager().popBackStack(); 
    }else { 
     super.onBackPressed(); 
    } 
} 

Fragment fragment = new LoginFragment(); 
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
fragmentTransaction.replace(R.id.container,fragment); 
fragmentTransaction.addToBackStack(null); 
fragmentTransaction.commit(); 
+0

何もない、アプリが戻るがその後閉じる – baldcl

+0

アプリが戻ってきたが閉じてしまうアプリケーションは、最初のバックプレスや何が終了した後になる? –

+0

はい、私はdifferents画面を通過することができますが、私は戻ってみると、希望の画面が表示されますがすぐに閉じる – baldcl

0

は、このコードを試してみてください。

@Override 
    public void onBackPressed() { 
     Fragment currentFragment = getSupportFragmentManager().getFragments().get(getSupportFragmentManager().getBackStackEntryCount() - 1); 
     if (currentFragment instanceof OnBackPressed) { 
      ((OnBackPressed) currentFragment).onBackPressed(); 
      return; 
     } 
     super.onBackPressed(); 
    } 
+0

何も、アプリのバックしかし、閉じる – baldcl

+0

私はMainActivityの条件が動作しないと思う。 OnBackPressedインターフェイスを正しく使用していることを確認します。例:OnBackPressed backPressed =新しいLoginFragment(); – Noorul

+0

"if"を削除しましたが、同じ結果が、 "戻るボタン"を押した後に下がり続けます – baldcl

0

この方法で、1つのインタフェースを作成します。

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; 
    } 
} 

を作成します。

関連する問題