Custom toolbar to back press icon and function in fragment android - 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();
}
});

Related

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

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);
}

How to return to previously opened tab on back button press

I have a TabLayout with 3 sections. Each section contains list of items.
When I click item on second section, I start new activity like this.
Intent intent = new Intent(getContext(), SecondDetailActivity.class);
startActivity(intent);
Inside of SecondDetailActivity I have an action bar with back button.
And I handle click of back button like this.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
onBackPressed();
}
}
return super.onOptionsItemSelected(item);
}
My problem is:
When I click device's back button, the selected tab of the tablayout is Second tab. But when I click on action bar navigation button, the selected tab is First tab.
How can I make action bar's behavior same as device's back button?
Try to return true while your case is android.R.id.home
like
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
onBackPressed();
return true;
}
}
return super.onOptionsItemSelected(item);
}

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

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;
}

Using actionbar home as up button, home activity saveInstanceState is always null

Activity A ===click button===> Activity B
When press back button, Activity A is not recreated.
When press home as up button, Activity A is recreated.
So I save state when A.onSaveInstanceState(Bundle outState)
, and use state when A.onRestoreInstanceState(Bundle savedInstanceState).
Saving and Using works fine (except home as up button)
.
However,
When pressed home as up button,
system recreate Activity A, and savedInstanceState is gone.
How can I use Saved Instance State?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// I do not want this...
// Home as up button is to navigate to Home-Activity not previous acitivity
super.onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
In the onCreate() enable the home button.
#Override
public void onCreate(Bundle savedInstanceState) {
...
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
In the onOptionItemSelected() method do this.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
This should enable Up navigation. If you want the parent activity to be restored with a savedInstanceState. You should set launchMode="singleTop" in the parent activity in Manifest file.
For more info check out http://developer.android.com/: Providing Up Navigation
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
I used finish() insteed of NavUtils;
As #Joachim mentioned in a comment, there is a difference between R.id.home and android.R.id.home. In my case, I have been using R.id.home, which did not work, and android.R.id.home did work.

Categories

Resources