how to go back from same activity to different using back arrow? - android

I have A activity that consists a list of countries.
When I clicked on one country it open B activity that consists a list of images. and when I clicked on one of them it open activity C.
A(OnClick)-->OpenB-->B(OnClick)-->OpenC
and when i press back arrow on app bar it should back to B activity
Anyone has any idea how do I implement this?
Please help me

simply add below line in your onCreate()
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true)
and then add this lines
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
hope this will help you!

In your backButton you just call finish();
Example
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) // Press Back Icon
{
finish();
}
return super.onOptionsItemSelected(item);
}

Related

Back button as Menu in fragment

I want to add a back button as menu in the left of the action bar in fragment. But I don't want the back arrow as my icon.
actionBar.setDisplayHomeAsUpEnabled(true);
The above code line gives the back arrow symbol. Instead i want to use some custom image. Also using that custom image should get it back to its previous activity.
I added something like:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
It worked for me.
Add this to your theme in styles.xml:
<item name="android:homeAsUpIndicator">#drawable/my_icon</item>
It will override the default icon. By default it uses the following id:
android.R.id.home
You can use this id to navigate back to your previous activity:
View homeView = getActionBar().getCustomView().findViewById(android.R.id.home);
homeView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v)
{
//Go back
}
});
or
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
//Return back to your activity!
return true;
}
return super.onOptionsItemSelected(item);
}
For more information check the official documentation at : https://developer.android.com/training/implementing-navigation/ancestral.html

Changing Activities with toolbar items

Sorry for my english :)
I have an item in the toolbar and when the user clicks on the item I would like it to go back to previous activity or go to any other activities that I choose.
Thanks for help
actually this is my code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.back)
{
Intent i = new Intent(listActivity.this,MainActivity.class);
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
Set your toolbar home button property,
toolbar.setDisplayHomeAsUpEnabled(true);
Try calling finish() method.
if(id==android.R.id.home)
{
this.finish();
return true;
}
As #Mahfa already said, calling finish() will always navigate you back where you came from, in your case, MainActivity.
In fact all of this answers really helped me in someways
but the exact code that worked for me is like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}

Custom toolbar to back press icon and function in fragment android

I have created one fragment mybillList.java with mutliple list. After i clicking any item, it will move to mybillDetails.java.
I don't want drawer menu on mybillDetails.java.In this fragment, i need back press icon(with function) instead of drawer menu.
mybillList.java
I have tried getSuppotActionBar().setDisplayHomeAsUpEnabled(true);
mybillDetails.java
If you meant closing the activity when clicked back button in action bar, try this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
if not, add some stuff instead of finish().
Try this it will help you...
mActionBar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
OnBackPressed();
}
});

how to provide proper back navigation with fragment on clicking the home button

I would like to provide back navigation while clicking on the Home button of Actionbar.
In the Host Fragment
((MainActivity)this.getActivity()).fragment_Actualités=new Fragment_Actualités();
fragmentTransaction.replace(R.id.content_frame,((MainActivity)this.getActivity()).fragment_Actualités);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
((MainActivity)this.getActivity()).fragmentManager.executePendingTransactions();
((MainActivity)this.getActivity()).supportInvalidateOptionsMenu();
In the Host Activity
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
int id=0;
if(id==item.getItemId()){
if(id== R.id.home)
{
this.onBackPressed();
return true;}
}
The super.OnBackPressed works fine when I click on the back button it goes back as expected
How can I make it worked ?
Try using
fragmentManager.popBackStackImmediate()
Try changing:
this.onBackPressed();
to:
onBackPressed();
and extends FragmentActivity if you don't do it already
Reading your code again I think found the issue, you are assigning 0 to id so when you compere with item.getItemId() always return false. Try with this code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == android.R.id.home) {
this.onBackPressed();
return true;
}
}
I finally resolved it,
the matter was that, I had 2 MenuItems, the Settings and the Home One's .
So if only write R.id.Home it won't work but when changed to android.R.id.Home it works !!!
with the same code

How can I get Android ActionBar to goto Home when I click upper left?

I am seeing the up caret but nothing happens when I click it. Where do i tell it to goto home and set which activity is Home?
You need the line:
getActionBar().setDisplayHomeAsUpEnabled(true);
put that in onCreate();
That will make it clickable. The you will need to handle the click event. This is done by overriding onOptionsItemSelected()
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
default:
return super.onOptionsItemSelected(item);
}
}
If you want to use NavUtils, (which I have seen used by the ADT plugin when it makes Activities) you can replace change the implementation like so:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Don't forget to
import android.support.v4.app.NavUtils;
Also, you can add this to your Activity node:
<activity
android:name=".SomeActivity"
android:label="#string/activity_label" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mypackage.MainActivity" />
</activity>
NavUtils is usually the better approach. For more information, see the official Android Guidelines.
In your onCreate you have:
getActionBar().setDisplayHomeAsUpEnabled(true);
And then use:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Updated to address comments below, which correctly point out my original answer was not correct for all cases. The "home/up" button have are supposed to take you the Home Activity or the Parent Activity, respectively. From the ActionBar documents --
By default, your application icon appears in the action bar on the
left side. If you'd like, you can enable the icon to behave as an
action item. In response to user action on the icon, your application
should do one of two things:
Go to the application "home" activity, or
Navigate "up" the application's structural hierarchy
Implementing "Home" is straightforward. Launch an intent to the home activity (usually your main launch activity) of your app, clearing the backstack to the existing instance if one exists--
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
"up" is similar, except you should use
getActionBar().setDisplayHomeAsUpEnabled(true);
in your onCreate to get the icon to show as "up" instead of "home", and launch an instance of the parent activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent parentActivityIntent = new Intent(this, MyParentActivity.class);
parentActivityIntent.addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(parentActivityIntent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
In both case, for Activities that can be launched from outside your application, you should add FLAG_ACTIVITY_NEW_TASK to launch into a new task instead of the callers task.
"Up" can get more complicated if your Activity can be launched from another app, and want to build a back stack for your application to get back to the root. See for this guide for more info.
My original answer, shown below, is considered bad form in the guide because it it treats the home/up button as 'back'. It will take you to the activity that invoked this activity, which is not necessarily the home or parent. It works well as 'up' for apps that have strict tree hierarchy of activities because an given activity was always launched by its parent.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Categories

Resources