回到调用另一个活动的片段

时间:2015-12-12 21:55:38

标签: java android android-fragments navigation onbackpressed

我的MainActivity有两个片段,比如fragment1和fragment2。现在,当活动开始时,默认片段是fragment1。然后我导航到fragment2,然后在某些事件更改中创建另一个名为CalledActivity的活动。我想要的是能够在CalledActivity的背面导航回到fragment2而不是fragment1。我怎么能做到这一点?

看了几个问题,似乎没有明确的答案。

public class MainActivity extends AppCompatActivity {
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;

   private Toolbar toolbar;

//for display of  name and email inside the navigation drawer header
private TextView name_header;
private TextView email_header;
private String nameChar;

AlertDialogManager alert = new AlertDialogManager();

// Session Manager Class
SessionManager session;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Session class instance
    session = new SessionManager(getApplicationContext());
    session.checkLogin();


    setContentView(R.layout.activity_main);


    /**
     *Setup the DrawerLayout and NavigationView
     */

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    mNavigationView = (NavigationView) findViewById(R.id.shitstuff);


    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    name_header = (TextView) findViewById(R.id.name_header);
    email_header = (TextView) findViewById(R.id.email_header);

    name_header.setText(Config.USER_FULL_NAME);
    email_header.setText(Config.USER_EMAIL);

    nameChar= Utils.parseName(Config.USER_FULL_NAME);

    TextDrawable drawable = TextDrawable.builder()
            .buildRound(nameChar, Color.parseColor("#FF6F00"));

    ImageView image = (ImageView) findViewById(R.id.image_view_name);
    image.setImageDrawable(drawable);

    /**
     * Lets inflate the very first fragment
     * Here , we are inflating the TabFragment as the first Fragment
     */

    mFragmentManager = getSupportFragmentManager();
    mFragmentTransaction = mFragmentManager.beginTransaction();
    mFragmentTransaction.replace(R.id.containerView, new PrimaryFragment()).commit();


    /**
     * Setup click events on the Navigation View Items.
     */
    mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            menuItem.setChecked(true);
            mDrawerLayout.closeDrawers();


            if (menuItem.getItemId() == R.id.nav_item_product) {
                FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.containerView, new ProductsFragment()).addToBackStack( "products fragment commit" ).commit();

            }

            if (menuItem.getItemId() == R.id.nav_item_home) {
                FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                xfragmentTransaction.replace(R.id.containerView, new UploadImageFragment()).addToBackStack( "home fragment commit" ).commit();
            }

            if (menuItem.getItemId() == R.id.nav_item_order) {
                FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                xfragmentTransaction.replace(R.id.containerView, new TabFragment()).addToBackStack( "orders fragment commit" ).commit();
            }


            return true;
        }

    });


    /**
     * Setup Drawer Toggle of the Toolbar
     */


    ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name,
            R.string.app_name);

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    mDrawerToggle.syncState();


}

@Override
public void onBackPressed() {
    if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
        getSupportFragmentManager().popBackStack();
    } else {
        this.finish();
    }
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            String stredittext = data.getStringExtra("edittextvalue");
            Log.d("xyz",stredittext);
        }
    }
}





}

1 个答案:

答案 0 :(得分:1)

您必须覆盖onOptionsItemSelected活动上的CalledActivity方法,例如

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            super.onBackPressed();
            return true;
    }
    return super.onOptionsItemSelected(item);
}