I set up Toolbar in my app (for the first time) but the I can't set the Home (/Up) button to do anything when clicked. I have 3 activitives (MainActivity , Second , Third) and I've defined the toolbar in all 3 but nothing happens when I click the Home button in each of the activitis I want the button to do his (in my understanding) default action which is go back to the Home Activity which is "MainActivity".
Here is some relevant code: Toolbar layout (named: 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>
my 3 activities layout 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
Actually all the 3 activities simply include the toolbar layout which names "app_bar.xml".
Here is part of my Mainfest file where I configured the app_parent:
<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>
and ofcourse parts of my java code for each of my 3 activities: 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:
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);
}
});
}
and ThirdActivity:
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);
}
You can see that all 3 Activities "onCreate" functions are pretty much the same.. When I start my app I can see the toolbar in all 3 activities and I can see the back arrow in all but clicking it do nothing. I know I can set the toolbar.setNavigationOnClickListener
but I just want it to make it default action which is go to home activity.... Thank you very much, Noam
Try this:
@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;
}
Try like this...
@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);
}
If you are having parent activity,Follow this code,
@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);
}
Try this in your activities
@Override
public boolean onOptionsItemIdSelected(int id) {
switch (id) {
case R.id.home:
//do stuff
break;
}
return true;
}
In your case, I recommend to you create a AbstractActivity which inherit all your activities. So you can factor a number of behavior , including management of the toolbar button
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.