2016-04-29 5 views
1

私のアプリにツールバーを設定しましたが(初めて)、クリックすると何かをするためのホーム(/上)ボタンを設定できません。 私は3つのアクティビティ(MainActivity、Second、Third)を持っていますが、3つのツールバーすべてを定義しましたが、アクティビティの各ボタンでHomeボタンをクリックすると何も起こりません。それは "MainActivity"であるHome Activityに戻ります。ここでツールバーの上ボタンは何もしません

は、いくつかの関連するコードです: ツールバーのレイアウト(命名:app_bar.xml):

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar_id" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/toolBar_background" 
     android:elevation="5dp" 
     android:title="@string/app_name" /> 
</RelativeLayout> 

私の3つの活動は、XMLSをレイアウト: Activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    tools:context="com.example.noamm_000.finaltoolbarproject.MainActivity"> 

    <include layout="@layout/app_bar"/> 
//More layout code... 

Activity_Second.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 

    tools:context="com.example.noamm_000.finaltoolbarproject.Second"> 

    <include layout="@layout/app_bar"/> 
//More layout code... 

Activity_Third.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.noamm_000.finaltoolbarproject.Third"> 

    <include layout="@layout/app_bar"/> 
//More layout code 

実際には、すべての3つのアクティビティには、単に「app_bar.xml」という名前のツールバーレイアウトが含まれています。

<activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".Second" 
      android:parentActivityName=".MainActivity"> 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value=".MainActivity"/> 
      <intent-filter> 
       <action android:name="com.example.noamm_000.finaltoolbarproject.Second" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".Third" 
      android:parentActivityName=".MainActivity"> 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value=".MainActivity"/> 
      <intent-filter> 
       <action android:name="com.example.noamm_000.finaltoolbarproject.Third" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 

と私の3つの活動のそれぞれのための私のJavaコードの一部ofcourseの: MainActivity:

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

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

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true); 

     mainBtn = (Button) findViewById(R.id.btn_main); 
     openSecondActivity(); 
    } 
    public void openSecondActivity(){ 
     mainBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent("com.example.noamm_000.finaltoolbarproject.Second"); 
       startActivity(intent); 
      } 
     }); 

SecondActivityここ

は私がapp_parentを構成し、私のMainfestファイルの一部です:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_second); 
     btnSecond = (Button) findViewById(R.id.btn_second); 

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

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true); 

     openThirdActivity(); 
    } 

    public void openThirdActivity(){ 
     btnSecond.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent("com.example.noamm_000.finaltoolbarproject.Third"); 
       startActivity(intent); 
      } 
     }); 
    } 

および第3のアクテビティ:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_third); 

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

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true); 
    } 

    public boolean onCreateOptionsMenu(Menu menu){ 
     getMenuInflater().inflate(R.menu.toolbar_menu,menu); 
     return super.onCreateOptionsMenu(menu); 
    } 

あなたはすべての3活動「のonCreate」の機能はほとんど同じであることがわかります。.. 私は私のアプリを起動すると、私はすべての3つの活動にツールバーを見ることができると私はすべてに戻る矢印を見ることができますが、クリックすると何もしません。 ノーム

答えて

0

を、あなたは親アクティビティがある場合

@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(); 
     if (id == android.R.id.home) { 
      Intent i = new Intent(CurrentActivity.this, HomeActivity.class); 
      i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(i); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
+0

ありがとう、それは働いたが、私はこのonOptionsItemSelected関数を使用して定義する必要はありません、このボタンのためのdefaullt関数が存在しないですか? – Noam

+0

@ Noam..itはこのボタンのデフォルト機能です。ツールバーの上ボタンがあなたの特定のタスクを定義する必要がある場合は、それを定義する必要があります... –

+0

@Noam私のコードはあなたを親に連れて行きますバックスタックを通過するのではなく、アクティビティを実行します。また、Intent.Flagを追加してバックスタックをクリアしました。ホームアクティビティに行くときに便利な機能です。ユーザーが 'Up'ボタンを使用しているときに、バックスタックが混乱するのを防ぐことができます。 –

0

はこの試してみてください私はtoolbar.setNavigationOnClickListenerを設定することができます知っているが、私はちょうどそれが家の活動に移動され、アクションをデフォルトにしたい.... はどうもありがとうございました、 :

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case android.R.id.home: 
       //finish(); or Do wahtever you wnat to do 
       return true; 
    } 
    return true; 
} 
+0

[こちら](http://developer.android.com/training/implementing-navigation/ancestral)をチェックしてください。html#NavigateUp) – Raghavendra

+0

@Noam wahtエラー? – Raghavendra

+0

ありがとう、それは働いたが、私はこのonOptionsItemSelected関数を使用して定義する必要はありません、このボタンのためのdefaullt関数が存在しないのですか? – Noam

0

は、このコードに従ってください...このようにしてみ

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()){ 
     case android.R.id.home: 
      if(NavUtils.getParentActivityIntent(this)== null) { 
       onBackPressed(); 
      }else{ 
       NavUtils.navigateUpFromSameTask(this); 
      } 
      break; 
    } 
    return super.onOptionsItemSelected(item); 
} 
+0

ありがとう、それは働いたが、私はこのonOptionsItemSelected関数を使用して定義する必要はありません、このボタンのためのdefaullt関数が存在しないのですか? – Noam

0

あなたの場合

@Override 
public boolean onOptionsItemIdSelected(int id) { 
    switch (id) { 
      case R.id.home: 
       //do stuff 
       break; 
    } 
    return true; 
} 

あなたの活動でこれを試してみてください、私はすべてのあなたの活動を継承AbstractActivityを作成し、あなたにお勧めします。したがって、ツールバーのボタンの管理など、いくつかの動作を考慮に入れることができます。

+0

これはAndroid.R.id.homeでなければなりません(何も起こりません)。それはうまくいきましたが、このボタンのdefaullt関数は存在しません。onOptionsItemSelected関数を使って定義する必要はありません。あなたのお勧めについて - 良いと思う、試してみる – Noam

+0

これはToolbarButton全体のデフォルト機能です:) –

関連する問題