簡體   English   中英

在導航抽屜中實現活動而不是片段

[英]implementing Activities instead of fragments in Navigation drawer

我一直在研究這個帶有活動的導航抽屜。 我的項目中有幾個活動,我想在使用導航抽屜之間切換。 到目前為止,我已經實現了這一點

  1. 創建導航抽屜並編寫一些活動代碼。

  2. 在所有活動中顯示相同的導航抽屜。

  3. 應用啟動時啟動默認活動。

但現在我的問題是,當我第一次打開我的應用程序時,它加載了我想要的活動,我可以看到這個導航抽屜。 當我切換活動並按下后退按鈕時,我收到了帶有導航抽屜的空白活動。 我知道它有點初學者級別的問題。 但請如果有人可以建議編輯。 這是我的代碼。

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

}

我在簡單的列表視圖類型導航抽屜中完美地運行了相同的應用程序。 但現在我正在嘗試實現花哨的材料設計。

嘗試覆蓋onBackButtonPressed方法。

清除/刪除 MyAct 和 About 類中的onBackpressed方法將帶回帶有任何填充內容的最后一個視圖。

但是,如果您想在按下后退鍵時打開其他視圖或啟動新活動,您可以在此onBackPressed方法中添加您的代碼

希望能幫助到你。 干杯!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM