2016-09-19 9 views
-1

私はアクティビティーを使用してこのナビゲーション・ドロワーで作業しています。私は、私のプロジェクトで、私はnav drawerを使用して切り替えるためにいくつかのアクティビティを持っています。 は、これまでのところ、私はこのナビゲーション・ドロワーのフラグメントの代わりにアクティビティーを実装

  1. はナビゲーション引き出しを作成し、いくつかの活動コードを書く達成しています。

  2. すべてのアクティビティで同じnav引き出しを表示します。

  3. アプリの起動時にデフォルトのアクティビティを開始します。

私のアプリが最初に開いたときに、自分の希望するアクティビティが読み込まれ、このナビゲーションドロワーが表示されるようになりました。私がアクティビティを切り替えて、戻るボタンを押すと、私はnav drawerでこの空のアクティビティを得ています。私はある程度の初心者レベルの問題を知っています。しかし、誰かが編集を提案できる場合は、どうかしてください。ここに私のコードです。

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

}

私は、単純なリストビュー型ナビゲーションの引き出しの中に完璧に働いて、この同じアプリVEの。しかし今、私は素晴らしい素材デザインを実装しようとしています。

+0

isLaunchとは何ですか? – brandall

答えて

0

onBackButtonPressedメソッドをオーバーライドしてみてください。

+0

編集してください。私はそれをどうやって行うのか分からない。 –

0

MyActおよびAboutクラスのonBackpressedメソッドをクリア/削除すると、最後に表示されたものがすべて読み込まれます。あなたには、いくつかの他のビューを開きたいか、バックキーが押されたとき、新たな活動を開始したい場合は

しかし、あなたは

はそれが役に立てば幸いこのonBackPressed方法でここにコードを追加することができます。乾杯!

関連する問題