Reputation: 37
I created a back button with this code
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
So that the user comes back to the parent Activity. But now I want that the user comes back to another Activity (not the parent activity). How can I do that ?
Upvotes: 1
Views: 1965
Reputation: 1279
Add this in manifest, for your activity name modify it
<activity
android:name="SecondActivity"
android:parentActivityName="ParentActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app_name.A" />
</activity>
Then you should add code like below in your Child or Second Activity no need to pass intent just finish()
current activity like below
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(SecondActivity.this,ParentActivity.class);
startActivity(about);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
Upvotes: 0
Reputation: 4335
Try this way,
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_file, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent about = new Intent(MainActivity.this,Target.class);;
startActivity(about);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
here
android.R.id.home
refer to your Back icon which is inActionBar
Upvotes: 0
Reputation: 8149
Handle actionbar home button pressed event and performed your logic
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// Your desired class
startActivity(new Intent(ThisActivity.this, NextActivity.class));
break;
}
return true;
}
Upvotes: 2
Reputation: 20910
You will have to override onBackPressed()
from your activity:
@Override
public void onBackPressed()
{
super.onBackPressed();
startActivity(new Intent(ThisActivity.this, NextActivity.class));
finish();
}
Note : In this code
ThisActivity
is your current Activity andNextActivity
is which you open Activity on Back Button Click.
Upvotes: 4