Get back to a fragment from an Activity

前端 未结 10 1899
野趣味
野趣味 2020-12-09 05:10

I have three fragment in an Activity C. they are behaving as tabs. I have to go from a fragment to a new Activity X. Now i want to come back to fragment from Activity X.

相关标签:
10条回答
  • 2020-12-09 06:08
    viewPager.setCurrentItem(tabPosition);
    

    This solution did wonders for me.

    0 讨论(0)
  • 2020-12-09 06:10

    if i want to go back to an Activity to another activity i override on back pressed and using intent call that actvity

    Normally, if you want to go back you don't have to do something. Pressing the device's hardware Back button will take you to the previous activity with the corresponding tab already opened.

    0 讨论(0)
  • 2020-12-09 06:10

    enter image description here

    @Override
    public void onBackPressed() {
      //Here "digiM" is an Actionbar Title
        if(getTitle().equals("digiM") || getSupportActionBar().getTitle().equals("digiM"))
        {
            //reload the activity if you want to open it again as your need
            Intent intent = new Intent(Your_Current_Activity.this,Your_Current_Activity.class);
            startActivity(intent);
        }
    }
    

    If you have action bar in that fragment, then you can check action bar title on onBackPressed() in Activity which contains method like.

    0 讨论(0)
  • 2020-12-09 06:12

    First, Override the back press to goto the activity where the fragments are :-

    @Override
    public void onBackPressed()
    {
    
        Intent intent = new Intent(CurrentActivity.this,ActivityYouLikeToGo.class);
    
        intent.putExtra("Check",1);
        startActivity(intent);
    }
    

    then goto the ActivityYouLikeToGo.java file and in onCreate do this:-

    Intent intent = getIntent();
    String s1 = intent.getStringExtra("Check");
    
    if(s1.equals("1"))
      {
    s1 = "";
    Fragment fragment = new YOURFRAMENTNAME();
    if (fragment != null) {
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(R.id.frame_container, fragment).commit();
      }
    }
    
    0 讨论(0)
提交回复
热议问题