I have an activity up-navigation enabled, so I have the android:parentActivityName set. This works.
However, I want my AppPreferencesActivity to be accessed from different parent activities. I'd like to have my up-action return to the activity that launched it. Removing the parentActivityName attribute removes the up action completely.
How can I configure the application so that Up Navigation returns me to the activity that launched my AppPreferencesActivity?
<activity
android:name="org.me.AppPreferencesActivity"
android:label="#string/title_activity_preferences"
android:parentActivityName="org.me.MainActivity">
</activity>
First, I removed the parentActivityNameAttribute.
Then, in my AppPrefernecesActivity, I added this to the creation portion:
getActionBar().setDisplayHomeAsUpEnabled(true);
THen, defined a case for handling the "home" action.
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
finish();
return true;
default:
return false;
}
}
This works, but is there a better way? This seems a bit hackish.
Related
How can I prevent the ActionBar back button (we gonna say ABBB) of my SecondActivity to recreate the MainActivitywhen clicked ?
I have a ListView in the MainActivity and it can be edited using the SecondActivity. The problem is that when the user presses the ABBB, the ListView is reset to default...
However, when the user clicks my OK button or presses physical back button, there is not any problem because I'm using finish();
#Override
public void onBackPressed() {
finish();
}
If I use this code... :
switch (item.getItemId()) {
case (android.R.id.home):
finish();
}
...there is the same problem because, I guess, this method is called after "Android's code".
How can I totally override Android ABBB's clicked code to set it to just call finish(); ?
The ideal scenario is, when are you in the SecondActivity (I take it that, this means that you are in Edit mode), and you press the device back button or ABBB, you show a subtle alert to the user saying "do they really want to dismiss the editing", or go ahead and apply the edit done as in the Contacts application.
So that being said, if all you require is to finish() the activity on press of ABBB, the code that you shown above should work fine (though you need to put return true; after finish()). It works for me. Try the below one in your activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed(); // or finish();
return true;
}
return super.onOptionsItemSelected(item);
}
I put onBackPressed(); because your ABBB with now imitate your device back button.
Or, you can set your parent activity's launch mode as -
android:launchMode="singleTop"
that will do all without changing anything.
Reference
Action Bar
I'm talking about the (Number 1, in the pic), button with a little arrow and the app icon and the top left side of the screen. It is automatically defined when we select the "Black activity" template.
My app has a pretty huge hierarchy graph, got about 25 activities now.
I'm basically just showing a few tutorials and one can navigate to it according to the categories.
Now that "Back" (?) button thingy on action bar is on every screen I have, and I do want to keep it. The code shows no error, but when I actually press that button, the app stops working.
What I want, is to just replicate the actual back button function with that (Number 1) button, that I showed in the image.
When I press it, the top most screen should close, and the last one should open. Just close the screen.
What I tried :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
this is the the function that inflates that buggy button along with the action bar. I tried to replace the entire code, and call "Finish" function, but that failed miserably.
I was unable to find a function specifically made for that top left most button...
I want the top most screen on the stack(the one in the foreground) to close, when this button is touched.
How to do this ?
I think the easiest way out is follows:
I am assuming that from activity A you are starting the activity B. Now from activity B you want to go back to activity A on pressing the top left back button on action bar. simply call this.finish() or ActivityName.this.finish() from there:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
This should finish your current activity. However if you have lot of activities, then you might have to do this in all the activities. To save yourself from that effort, you can make a class lets call it AbstractActivity; that extends Activity. Then you can extend all your other activity classes to extend that class(AbstractActivity). Inside AbstractActivity you can put the above code. So now that piece of code would be valid for all your activities and that feature would be implemented for all of them. Basically this sort of thing (Inheritance)can be used any time, when there are some common features which would be applicable to your many classes.
If you are receiving any errors, please do post your LogCat if you require further help.
Hope this helps you.
just giving basic code given by #shobhit puri...
for invoking the action bar back button..add the following code in the onCreate() method along with the onOptionsItemSelected....
protected void onCreate(Bundle savedInstanceState)
{
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
At the moment it is main activity but I want to change it to the Categories activity.
Is this where the problem lies?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
Go to AndroidManifest.xml, find your Sport activity and add this code
<activity android:name="Sport" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="PARENT_ACTIVITY_PATH" />
</activity>
Substitute PARENT_ACTIVITY_PATH with the name of your parent activity adding also your package name (e.g. com.example.Categories).
Going off #Mangusto. After adding the parent activity path to your AndroidManifest add this line in the onCreate method of your Sport activity getSupportActionBar().setDisplayHomeAsUpEnabled(true);
I want to customize the activity back button in action bar, not in hard key back button. I have overriden the onBackPressed() method. It works with my emulator back button, but not with action bar back button.
I want it to happen with action bar. How can I do this?
Here is my code:
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
return;
}
I have used this toast whether back pressed is working or not but the actual implementation changes like to move back to previous activity. But this is not working with the button present on top of action bar (besides title of the activity).
Please any one could specify me the problem.
I think you want to override the click operation of home button. You can override this functionality like this in your activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
If you want ActionBar back button behave same way as hardware back button:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return false;
}
Two things to keep in mind that the user can either press back button or press the actionbar home button.
So, if you want to redirect him to the same destination then you can do this.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return false;
}
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(intent);
finish();
}
This will take the user to the intent pressing either key or the action bar button.
Sorry mine is a late answer, but for anyone else arriving at this page with the same question, I had tried the above:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
if (item.getItemId() == android.R.id.home) {
....
}
....
}
but this failed to catch the "Back" button press.
Eventually I found a method that worked for me on https://stackoverflow.com/a/37185334/3697478 which is to override the "onSupportNavigateUp()" as I am using the actionbar from the "AppCompatActivity" support library. (There is an equivalent "onNavigateUp()" for the newer actionbar/toolbar library.)
#Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
and I removed the "android:parentActivityName=".MainActivity" section from the manifest file.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return true;
}
(1) Add Parent activity for your child activity (AndroidManifest.xml)
<activity
android:name=".ParentActivity" />
(2) override the onSupportNavigateUp method inside the child activity
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return false;
}
I have achieved this, by using simply two steps,
Step 1: Go to AndroidManifest.xml and in the add the parameter in tag - android:parentActivityName=".home.HomeActivity"
example :
<activity
android:name=".home.ActivityDetail"
android:parentActivityName=".home.HomeActivity"
android:screenOrientation="portrait" />
Step 2: in ActivityDetail add your action for previous page/activity
example :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
If you want to return to the previous instance of an Activity by pressing of ActionBar home button, without recreating it, you can override getParentActivityIntent method to use the one from the back stack:
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#Override
public Intent getParentActivityIntent() {
return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
EDIT:
Also you can achieve the same result by
setting the launchMode of your parent activity to singleTop.
So setandroid:launchMode="singleTop" to parent activity in your manifest.
Or you can use flag FLAG_ACTIVITY_CLEAR_TOP with the UP intent.
reference: Providing Up Navigation
#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) {
onBackPressed();
return true;
}
//noinspection SimplifiableIfStatement
if (id == R.id.signIn) {
return true;
}
return super.onOptionsItemSelected(item);
}
///////////////////
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
There are several ways how to set up back button in bar:
1) method .setDisplayHomeAsUpEnabled(true); will do it, and then you can simply override android.R.id.home
2) adding <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="my.package.parrent" /> in Android Manifest, but in this case you can not override android.R.id.home in OnOptionsMenuSelected.
.. for those who wonder why it doesn't work for them...
I'm Trying to open a second screen for my application. The user have to click on a menu item and then the screen must open. But what i try it always just ignores the rule after the case statement.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.OpenScreen: startActivity(new Intent(this, SecondScreenActivity.class));;
break;
}
return true;
I've also added the activity to AndroidManifest.xml inside the tag.
Any help will be apreciated
Solution
I had the finish() in the constructor of the new class.
For me your java code is correct and work for me that way.
However when I learned Android, when I came at the point of starting a new activity I faced the same problem as you, nothing was happening, without any error, simply not working.
In my case I forgot to add the activity in the AndroidManifest.xml file. Perhaps it is the same for you.
Your AndroidManifest file should contain somewhere something like that
<activity
android:name=".SecondScreenActivity" >
</activity>
this tell the system that SecondScreenActivity exitst.
In
http://developer.android.com/guide/topics/manifest/activity-element.html they say :
All activities must be represented by elements in the manifest file. Any that are not declared there will not be seen by the system and will never be run.
Hope this helps
try like this
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.OpenScreen:
startActivity(new Intent(YourClass.this, SecondScreenActivity.class));
break;
}
return true;
Don't return true for onOptionsItemSelected.
call super method like this.
return super.onOptionsItemSelected(item);
instead of return true;