简体   繁体   中英

Android (java): OnBackPress() closes my app when trying to go back from Activity to fragment

So far I had gone from fragment to the DetailActivity, now I added back button to take me back to the previous fragment. I tried the below code but the app keeps closing instead of going back to the fragment. Both of these code snippets are from my code.

Fragment.java

public class Fragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {


    String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
    
    private Button btnNext;
   


    public Fragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        btnNext = view.findViewById(R.id.add);
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(),
DetailActivity.class);
        startActivity(intent);
        getActivity().onBackPressed();
            }
        });
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return null;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

    }

And this is my onBackPressed() from DetailActivity.java method:

     public void onBackPressed(){
                FragmentManager fm = getFragmentManager();
                if (fm.getBackStackEntryCount() > 0) {
                    Log.i("DetailActivity", "popping backstack");
                    fm.popBackStack();
                } else {
                    Log.i("DetailActivity", "nothing on backstack, calling super");
                    super.onBackPressed();
                }
            }

DetailActivity.java:

    public class DeatilActivity extends AppCompatActivity {

   
    
    Button btnBack;
    


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

        
       
        btnBack = findViewById(R.id.btnBack);

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                onBackPressed();

            }
        });

    }

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

    @Override
    public void onBackPressed(){
        FragmentManager fm = getSupportFragmentManager();
        if (fm.getBackStackEntryCount() > 0) {
            Log.i("DetailActivity", "popping backstack");
            fm.popBackStack();
        } else {
            Log.i("DeatilActivity", "nothing on backstack, calling super");
            super.onBackPressed();
        }
    }

}

Any idea? :(

According to https://developer.android.com/reference/android/app/Activity , calling onBackPressed finishes the current activity, so it won't be there to return to if called from the launching activity. Removing this call seems to fix the issue seen here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM