Back button behavior on an Activity - android

I have a problem in my app. I have a MainActivity with Fragments, each Fragment have a list with a recyclerView. When I click an Item of the list, the app goes to a new DetailActivity.
In the DetailActivity I have the next code line:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
When I click the back button in the ActionBar I return to the MainActivity in another fragment, not the one I clicked the item.
Thanks!

As stated by #kareem adel make sure you are not ending the previous activity. Dont use Flag_activity_clear_task or flag_activity_new_task
If that doesn't help you could just tell the back button where to go.
With getSupportActionBar().setDisplayHomeAsUpEnabled(true); set you can do this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
else if(id == android.R.id.home){
//Back button was hit so go somewhere.
Intent i= new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); //Clear previous Activities
startActivity(i);
finish(); //End this activity.
return true;
}
return super.onOptionsItemSelected(item);
}

Related

Start Activity in same intent

i want to know how to start all my activities in same intent. My app have a drawer activity and i want when i select any item, it will start an activity in the same intent, how can i do that ?
Main Acitivy
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
startActivity(new Intent(MainActivity.this, Main2Activity.class));
return true;
I checked Google Drive and I'm guessing you're referring to the behavior that when you click on something only a part of the screen updates. You can easily implement this by using Fragments. I cannot explain it to you in detail but this is a developer.google link that will help you get started. https://developer.android.com/training/basics/fragments/index.html

How to use a navigation drawer in Android Studio?

First time here.
I need to use Navigation drawer for my app. But im working with Activities.
The question is, how can i select an item and redirect to an activity?
Thank you
If you used the Navigation Drawer Template from Android Studio, you will have a main activity with a method onNavigationItemSelected. Implement the method to check the id of the menu item passed to it and decide which Activity to start based on the menu id. For example:
public boolean onNavigationItemSelected(MenuItem item){
int id = item.getItemId();
if (id == R.id.nav_settings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
}else if (id == R.id.nav_faq) {
startActivity(new Intent(this, FAQActivity.class));
return true;
}
}

On home button click(in appbar/toolbar) from next activity, I need same fragment on main activity(which was there before going to next activity)

I have a fragment on my main activity which gets loaded on click of item from nav drawer and on click of a list item from the fragment(main screen) I navigate to another activity. If I press the device back button I get my fragment screen(as expected) but if I use a tool bar and enable the the home button by saying
getSupportActionBar().setHomeButtonEnabled(true);
I get my main activity content which was prior to selecting an item from drawer. I have set parent of next activity as the main activity but I need the fragment loaded when i go back.
For my problem I found a solution. on click of the home button in the toolbar I killed my activity using finish() method and it worked just as i wanted. Hope it helps the needy.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if(id == android.R.id.home)
{
finish(); //kill subActivity so as to get same screen which was before going to subActivity
}
return super.onOptionsItemSelected(item);
}
Try this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.home) {
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);
}
You need to add this getSupportActionBar().setDisplayHomeAsUpEnabled(true);
along with getSupportActionBar().setHomeButtonEnabled(true);

Evaluating R.id.home

I was following this tutorial here - http://developer.android.com/training/implementing-navigation/ancestral.html - for implementing the Up Navigation. Sort of the same as the user pressing the Back button on the phone, but the onBackPressed() method does not fire when the 'Up' button is pressed. In the tutorial they show that you trap R.id.home in the onOptionsItemSelected() method. This web page - http://developer.android.com/reference/android/R.id.html - shows that the value of R.id.home should equal 16908332, but it doesn't in my app. In the code below if I use R.id.home it fails. If I hard-code in 16908332 it works. For me R.id.home evaluates to 21312330724. According to the page, all of the R.id values begin 1690. I hate hard-coding in a value for a built-in value, but I'm not sure what else to do. Could this cause problems down the road? Am I doing something wrong? Is this a bug?
Greg
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
//noinspection SimplifiableIfStatement
if (id == R.id.home) {//16908332
Intent upIntent = NavUtils.getParentActivityIntent(this);
upIntent.putExtra(CAT_ID, CatID);
upIntent.putExtra(USER_ID, UserID);
upIntent.putExtra(LIST_ID, ListID);
setResult(RESULT_OK, upIntent);
NavUtils.navigateUpTo(this, upIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
You need to compare it to the android one:
if (id == android.R.id.home){
...
}

Dont know why my up button is not wotking

Here i setup this on oncreate method of second avticity
super.onCreate(savedInstanceState);
// Actionbar
getActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.new_message);
And this is onOptionsItemSelected
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_save) {
Intent newMessage = new Intent(getApplicationContext(),NewMessage.class);
startActivity(newMessage);
}
if(id == R.id.home){
Toast.makeText(getApplicationContext(), "Home button click", 2000).show();
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
/* return super.onOptionsItemSelected(item);*/
return true;
}
Is there anything i need to change somewhere else like Manifest or some othere there is no code for back activity on MainActivity
problem:
if(id == R.id.home)
You are using the id from your R java of your project which will definitely return false it is supposed to be the native android home not your R generated id for home.
solution:
if(id == android.R.id.home)

Categories

Resources