How to up navigate from fragment activity? - android

Hi I am having trouble while going back to parent activity from fragment activity. I want back arrow at top left corner inside the action bar.
I am able to show it in action bar activity using this code
getSupportActionBar().setDisplayShowHomeEnabled(true);
But I am not able to do it in tabbed activity's fragment.
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
Please Help me!!

Add this inside onCreate() of your tabbed activity,
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
And for navigating back, you need to override following method in your tabbed activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
Happy coding.

Add this method to your activity to navigate back.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();// or the action you want to do eg. Removing fragment
break;
}
return super.onOptionsItemSelected(item);
}

Related

Back navigation in title bar - fragment

There are many questions like this asked but everything I have tried seems to not work.
Essentially I have a main activity that calls different fragments depending on what the user clicks with the home fragment being default.
I would like to have a back button on the title bar, to go back to the previous fragment.
My fragment is called from the main activity like so:
Fragment fragment = null;
fragment = new nextFragment();
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, fragment).addToBackStack(null);
fragmentTransaction.commit();
fragmentTransaction.addToBackStack(null);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
But since ActionBarActivity activity is deprecated I need to extend AppCompatActivity instead of FragmentActivity so I can use actionbar (I'm assuming this is what I need).
However then I am unable to switch to my fragment. So does anyone know how I could implement a back button in my fragment or how to use AppCompatActivity in this situation.
Thanks for any help.
Please try this if you extend AppCompatActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Include these 2 lines ONLY if need to use Toolbar from layout xml as Action Bar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Add back navigation in the title bar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//
//Other works to be done in onCreate.....
//
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
//Title bar back press triggers onBackPressed()
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
//Both navigation bar back press and title bar back press will trigger this method
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0 ) {
getFragmentManager().popBackStack();
}
else {
super.onBackPressed();
}
}
}
To add Back Button in title bar, you must add the following code to your Fragment.
Toolbar toolbar = (Toolbar)view.findViewById(R.id.app_bar);
AppCompatActivity AppCompatActivity = (AppCompatActivity)getActivity();
AppCompatActivity.setSupportActionBar(toolbar);
AppCompatActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
return view;
}
Don't forget to extend your MainActivity to AppCompatActivity.
You must then use this Java code in my Fragment class to react to the user tapping the back/up icon in the action bar.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You've saved the last fragment used by calling
addToBacktack(null).commit()
So the next step forward to call it is by overriding onBackPressed() in the activity hosting the fragment.
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0 ){
getFragmentManager().popBackStack();
}
else {
super.onBackPressed();
}
}
So whenever you call the activity's onBackPressed() from the fragment, the fragment would go back to the last saved fragment.

Android how to create fragment with X button

Hey community have a good day. I need to create some ui like that :
I tried : Create a fragment , create a dialog .
Assume that i have an activity A.I do some work in my activity then i start this "New event " ui.
I do some work and click "X" button and wanna continue from where i left in my activity A. So how i can do this . This layout is fragment or dialog ?
Actually we can create new activity and pass intent to it and change back icon like that :
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.X_icon);
}
And these 2 methods :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return(super.onOptionsItemSelected(item));
}
#Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.nothing,R.anim.slide_out_right);
}
Doing like that if we press back button or press X button previous activity will not recreate.

Dynamic parent activity for Android up \ back navigation in Preferences menu

I've a Preference activity for a options menu in my Android app.
I've enabled the Up \ Back navigation on the ActionBar and I need to come back to the previous activity that called the Options menu.
For the Preference activity, I could use in the manifest:
android:parentActivityName="mypackage.com.MainActivity"
but how come back to other activies ? The Options menu is called from 4 different activities.
public class Prefs extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
ActionBar actionBar = getActionBar();
// Enabling Up / Back navigation
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
Make sure that you have declared the parent activity in the manifest like this...
<activity
android:name="com.myapp.SetPreferenceActivity"
android:parentActivityName="com.myapp.MainActivity"
>
and then make sure to add the case into your onOptionsItemSelected method...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
//Take me back to the main activity
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
if you prefer to have the up button point to a custom activity, you can just use an intent.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
Intent changeActivity = new Intent(this,OtherActivity.class);
startActivity(changeActivity);
return true;
}
return super.onOptionsItemSelected(item);
}
The default behaviour of the Back button is that it will get you back to the calling activity. The system maintains a back stack of activities while the user navigates the application. Do you need to override this functionality? Please be more specific.
Solved in this way:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
}
return true;
}

How can i set back navigation button in my action bar?

I am using actionBar.setDisplayHomeAsUpEnabled(true); for back navigation functionality but it is not working as properly as i need it and when i am using back button of my phone it works properly.
Is there any way to code on a button such that it work same as back navigation button of phone?
Along with
actionBar.setDisplayHomeAsUpEnabled(true);
or
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
you can try using this in Activity, too:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
So your menu item "Home"'s click will be handled using that.
I know this has been answered, but i always do this in my method when having the same back-button-like capability in another ActionBarActivity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case android.R.id.home:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
and ofcourse:
actionBar.setDisplayHomeAsUpEnabled(true);
You may/may not used this, its up to you :)

Back Arrow in Action bar Sherlock is not displaying

In my project I have used action bar sherlock library. I want to make back button in action bar I used following code
getSupportActionBar().setHomeButtonEnabled(true);
And
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
default:
return super.onOptionsItemSelected(item);
}
return false;
}
Every thing is working fine, but back Arrow (to the left of icon) is not displaying. I want to show Back Arrow also. Can anyone tell me what I am doing wrong.
You have to set it up this way:
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
Hope that helps.

Categories

Resources