I have a back button in one activity and when I tap on it to return to the parent activity it will reset the parent activity. It's like onCreate() is being called again. I'm not sure why that is because when you tap on the back button it just calls finish() to exist the activity I'm currently in.
Here is how I'm declaring the toolbar:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_item);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
if(getSupportActionBar() != null)
{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
This is what happens when you tap on the button:
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId()==android.R.id.home)
{
finish();
}
return super.onOptionsItemSelected(item);
}
The strange this is that when I hit the save button I return to the parent activity without any reset. So I'm not sure why this is happening.
You need to return true or else it will always call the onCreat() method. Also, you can create an "empty" intent and just not process it on the activity you return true.
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId()==android.R.id.home)
{
Intent intent = new Intent();
setResult(Intent_Constant.TAPPED_BACK_BUTTON, intent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Related
I need set back icon in my toolbar. I created my toolbar using android.support.v7.widget.Toolbar and for back using this code first set onClick to my back icon by this code:
ImageView backIcon = findViewById(R.id.back_icon);
backIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(CreateNewItem.this,MainActivity.class);
startActivity(intent);
finish();
}
});
and add this code:
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
it's work and back to activity but my problem it's when using back icon and back to activity see recyclerView is empty and activity not have data. what is my problem?
If You are using Toolbar then no need to set custom back icon, you have to just override this method
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
onBackPressed();
}
return (super.onOptionsItemSelected(menuItem));
}
and enable Toolbar:
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Happy coding!!
so i have Activity A,B, and C. Activity A & B both go to activity C. When i am on activity C and I press the back home button on mySupportActionBar, I want to return to the state of activity (from the state i left it in) I came from. How would i accomplish this?
Here is my onOptionsItemSelected(). So currently, it goes back to the designated parent activity i assigned in manifest to avoid my app from crashing. Because the parent activites require strings from intents.
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if(id == android.R.id.home)
{
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);
}
I would also love to accomplish this onBackPressed().
The back arrow button is the "home" button when you're in a inner activity so you could finish the inner activity or maybe just call the back button
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Use android.R.id.home
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You also can use setNavigationOnClickListener on toolbar to trigger back button.
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filter_category);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
//back button action
toolbar.setNavigationOnClickListener(view -> finish());
}
}
This question already has answers here:
When toolbar back button is pressed
(4 answers)
Closed 6 years ago.
how can I create a code for my android app where When I pressed the back button on the toolbar (Action Bar) some code will happening.
I tried but it does not work.
Added in main activity. onbackpress method
#Override
public void onBackPressed() {
super.onBackPressed();
stopActivityTask();
}
You can Try this way..
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// this takes the user 'back', as if they pressed the left-facing
triangle icon on the main android toolbar.
// if this doesn't work as desired, another possibility is to call
stopActivityTask(); // finish() here.
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
If Not Work
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// Title and subtitle
toolbar.setTitle(R.string.about_toolbar_title);
toolbar.setNavigationIcon(R.drawable.ic_action_back);
toolbar.setNavigationOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
stopActivityTask();
finish();
}
});
private Toolbar toolbar;
Then add this in your onCreate method
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Then you add these lines :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
/*
ADD WHAT YOU WANT TO DO WHEN ARROW IS PRESSED
*/
return super.onOptionsItemSelected(item);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
// click on icon to go back
//triangle icon on the main android toolbar.
if (id == android.R.id.home) {
//your code
return true;
}
return super.onOptionsItemSelected(item);
}
Right off the top, I want to clarify something: The button that I am struggling with is NOT the back button. I am referring to the up/home button in the ActionBar / Toolbar at the top of the app, not the Android button at the bottom. There are a few posts of a similar nature, but they address the back button, not the up button.
Here is the situation: I have an Activity A that has a ListView fragment. When the user clicks on a list view item, it launches Activity B. Pretty typical. Activity A has an EditText field in the toolbar that allows the user to enter a search parameter. If the user hits the up/home button from Activity B, I return successfully to Activity A. However, I want Activity A to show the same text in the EditText field that was there when they left it. If the user hits the back button, this text is restored. But if they navigate with the up/home button, the EditText field is empty.
Using some log statements, I can see that when a list item is tapped from activity A, onSaveInstanceState and onStop are both called (but onDestroy is NOT called at that point.) From activity B, when the up/home button is tapped, onDestroy from activity A is immediately called, followed by onCreate, etc. However, the bundle savedInstanceState is null, presumably since onDestroy was just called.
Why is onDestroy called when returning to Activity A? This makes no sense to me. Here is what I have in the manifest:
<activity
android:name=".Activity.ActivityA"
android:label="#string/app_name"
android:parentActivityName=".Activity.ParentActivity"
android:theme="#style/AppTheme"
android:launchMode="singleTop"
android:windowSoftInputMode="stateVisible" />
<activity
android:name=".Activity.ActivityB"
android:label="#string/app_name"
android:parentActivityName=".Activity.ActivityA"
android:theme="#style/AppTheme" />
Here are the relevant methods in Activity A:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
if (actionBar != null)
initializeActionBar();
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Log.d(TAG, "on create");
if (savedInstanceState != null) {
Log.d(TAG, "saved instance state not null");
if (savedInstanceState.getString("search_text") != null)
etSearch.setText(savedInstanceState.getString("search_text"));
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("search_text", etSearch.getText().toString());
Log.d(TAG, "on Save instance state");
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.d(TAG, "on restore instance state");
if (savedInstanceState != null) {
if (savedInstanceState.getString("search_text") != null)
etSearch.setText(savedInstanceState.getString("search_text"));
}
}
#Override
protected void onResume() {
super.onResume();
Log.d(TAG, "on resume");
}
#Override
protected void onStop() {
super.onStop();
Log.d(TAG, "on stop");
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "on destroy");
}
private void initializeActionBar() {
actionBar.setCustomView(R.layout.actionbar_with_edittext);
etSearch = (EditText) actionBar.getCustomView().findViewById(R.id.actionbar_searchfield);
etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
initiateNewSearch();
etSearch.clearFocus();
}
return false;
}
});
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(etSearch, InputMethodManager.SHOW_IMPLICIT);
etSearch.requestFocus();
}
I don't think that any of the code in Activity B is relevant here.
This is my console output when a user taps on a listview item in Activity A:
on Save instance stateon stop
And then this is what is generated when the user taps on the up/home button from activity B:
on destroy on create on resume
If there is anything else that may be of help, please let me know. Thanks for any advice!
I don't know why default up button implementation creates a new activity but a working solution for me is to override onOptionsItemSelected:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id== android.R.id.home ){
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
Also this solution works for me:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = NavUtils.getParentActivityIntent(this);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
NavUtils.navigateUpTo(this, intent);
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
I had this problem too, and after a little more digging found the correct solution is to make a change in the manifest to add the following to your parent activity:
android:launchMode="singleTop"
Details are explained here.
I have an app with tab navigation between fragments. One of these fragments has an option to open a new activity. When I use the built in device back button from this activity it goes back to the tabbed activity with the previous fragment tab selected.
I have added a back button to the action bar of an activity in my app by using:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and setting the parent activity in the manifest, but this button always navigates back to the first tab of the parent activity, rather than the one that was previously visible.
How can I make this back button behave in the same way as the device back button?
Do something like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
onBackPressed() method:
#Override
public void onBackPressed() {
super.onBackPressed();
}
Handle back event in this manner
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
in back press method
#Override
public void onBackPressed() {
Intent intent = new Intent(SecondActivity.this,TabbedActivity.class);
intent.putExtra("IsBack",true);
startActivity(intent);
}
in your tabbed activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabs);
if(getIntent().getExtras().getBoolean("IsBack")){
//navigate to your desire fragment
}
}