简体   繁体   English

在导航抽屉中实现活动而不是片段

[英]implementing Activities instead of fragments in Navigation drawer

I have been working on this nav drawer with activites.我一直在研究这个带有活动的导航抽屉。 I have a couple of activites in my projects that i want to switch between using the nav drawer.我的项目中有几个活动,我想在使用导航抽屉之间切换。 So far I have achieved this到目前为止,我已经实现了这一点

  1. Create Nav drawer and Write some activities code.创建导航抽屉并编写一些活动代码。

  2. Show same nav drawer in all activities.在所有活动中显示相同的导航抽屉。

  3. Start a default activity when app starts.应用启动时启动默认活动。

But now my issue is that when i open my app for first time, it loads my desired activity and i can see this nav drawer.但现在我的问题是,当我第一次打开我的应用程序时,它加载了我想要的活动,我可以看到这个导航抽屉。 when i switch activity and press the back button, I'm getting this blank activity with nav drawer.当我切换活动并按下后退按钮时,我收到了带有导航抽屉的空白活动。 I know its some kinda beginner level issue.我知道它有点初学者级别的问题。 But please if someone can suggest the edit.但请如果有人可以建议编辑。 Here is my code.这是我的代码。

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

NavigationView navigationView = null;
Toolbar toolbar = null;
DrawerLayout drawer;
FrameLayout frameLayout;

private static boolean isLaunch = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    frameLayout = (FrameLayout) findViewById(R.id.contant_frame);
    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

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

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

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

    if (isLaunch) {
        isLaunch = false;
        startActivity(new Intent (this,MyAct.class));
    }

}

@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) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

   if (id == R.id.nav_manage) {

        startActivity(new Intent(this,About.class));

    } else if (id == R.id.nav_share) {

        startActivity(new Intent(this,About.class));

    }

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

} }

I ve this same app working perfect in simple listview type nav drawer.我在简单的列表视图类型导航抽屉中完美地运行了相同的应用程序。 but now im trying to implement fancy material design.但现在我正在尝试实现花哨的材料设计。

尝试覆盖onBackButtonPressed方法。

Clearing/removing the onBackpressed method in MyAct and About class will bring back the last view with anything that was populated.清除/删除 MyAct 和 About 类中的onBackpressed方法将带回带有任何填充内容的最后一个视图。

But if you want to open some other view or want to start new activity when back key is pressed, you can add your code here in this onBackPressed method但是,如果您想在按下后退键时打开其他视图或启动新活动,您可以在此onBackPressed方法中添加您的代码

Hope it helps.希望能帮助到你。 Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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