如何从第二个导航抽屉活动中调用单个片段

时间:2017-02-06 08:11:24

标签: android

我有一个主导航抽屉,我称之为登录片段。还有其他片段我可以轻松调用。但是当我登录时,第二个导航抽屉打开了,我无法调用相同的碎片,这些碎片在使用主导航抽屉时可以轻松调用。

这是我的主要活动,其中导航抽屉被称为

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private View.OnClickListener mOnClickListener;
    SessionManager session;
    private NavigationView navigationView;

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

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

        session = new SessionManager(getApplicationContext());



        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        Fragment homeFragment;
        if(!session.isLoggedIn()){
            homeFragment = new SigninFragment();
            getSupportFragmentManager().beginTransaction().replace(R.id.content_main, homeFragment, String.valueOf(homeFragment.getClass())).addToBackStack(null).commit();
            getSupportActionBar().setTitle(getResources().getString(R.string.signin_title));
        }


    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        Fragment fr;
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("productAga");

        int id = item.getItemId();

        switch (id){
            case R.id.home_nv:
                fr = new HomeFragment();
                supportFragmentManager.beginTransaction().replace(R.id.content_main, fr, String.valueOf(fr.getClass())).addToBackStack(null).commit();
                supportActionBar.setTitle(getResources().getString(R.string.home_title));
                break;

            case R.id.signup_nv:
                fr = new SignupFragment();
                supportFragmentManager.beginTransaction().replace(R.id.content_main, fr, String.valueOf(fr.getClass())).addToBackStack(null).commit();
                supportActionBar.setTitle(getResources().getString(R.string.signup_title));

                /*Intent it  = new Intent(getApplicationContext(), SignupActivity.class);
                startActivity(it);*/
                break;
            case R.id.signin_nv:
                fr = new SigninFragment();
                supportFragmentManager.beginTransaction().replace(R.id.content_main, fr, String.valueOf(fr.getClass())).addToBackStack(null).commit();
                supportActionBar.setTitle(getResources().getString(R.string.signin_title));
                break;

            case R.id.about_nv:
                fr = new AboutusFragment();
                supportFragmentManager.beginTransaction().replace(R.id.content_main, fr, String.valueOf(fr.getClass())).addToBackStack(null).commit();
                supportActionBar.setTitle(getResources().getString(R.string.about_title));
                break;



            case R.id.logout_nv:
                session.logoutUser();
                Intent it  = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(it);
                break;

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

现在,当我登录时,我正在使用此代码,并注意到有相同的aboutus片段

public class LoggedinMainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    SessionManager session;
    private String gname;
    private String gemail;
    private static final String TAG = "TAG_LOG_IN" ;

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

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

        session = new SessionManager(getApplicationContext());



        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerlg_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();


        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_login_view);
        navigationView.setNavigationItemSelectedListener(this);

        /*Fragment homeFragment;
        if(session.isLoggedIn()){
            homeFragment = new DashboardFragment();
            getSupportFragmentManager().beginTransaction().replace(R.id.content_main, homeFragment, String.valueOf(homeFragment.getClass())).addToBackStack(null).commit();
            getSupportActionBar().setTitle(getResources().getString(R.string.signin_title));
        }*/

        session.checkLogin();

        HashMap<String, String> user = session.getUserDetails();
        gname = user.get(SessionManager.KEY_NAME);
        gemail = user.get(SessionManager.KEY_EMAIL);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.loggedin_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        Fragment fr;

        FragmentManager supportFragmentManager = getSupportFragmentManager();
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("productAga");

        int id = item.getItemId();

        switch (id){
            case R.id.homelg_nv:
                fr = new HomeFragment();
                supportFragmentManager.beginTransaction().replace(R.id.content_main, fr, String.valueOf(fr.getClass())).addToBackStack(null).commit();
                supportActionBar.setTitle(getResources().getString(R.string.home_title));
                break;

            case R.id.aboutlg_nv:
                Log.d(TAG +"_ABOUT", ": about");
                fr = new AboutusFragment();
                supportFragmentManager.beginTransaction().replace(R.id.content_main, fr, String.valueOf(fr.getClass())).addToBackStack(null).commit();
                supportActionBar.setTitle(getResources().getString(R.string.about_title));
                break;



            case R.id.logout_nv:
                session.logoutUser();
                Intent it  = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(it);
                break;

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerlg_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

我也在使用共享偏好进行会话管理以登录

此外,当我点击关于我们的

时,我遇到了android监视器
java.lang.IllegalArgumentException: No view found for id 0x7f0d008a (com.roomi.productagaapplication:id/content_main) for fragment AboutusFragment{35e39e5d #0 id=0x7f0d008a}

0 个答案:

没有答案