App home button not preserving data - android

On my apps home page I have an option to add items to a ListView using adapter.add(string). I also have another Activity that my app goes to using startActivity(intent). When I am in that second Activity and I press the back button all of the data that was added to the list is still there, however when I press the icon at the top left all the data is gone. Is there a way to make it so that the button for the icon preserves the data in my list. I think I should note that I don't want the data to be preserved when the app is closed, only when navigating through the open app.
This is my current method that handles the home button action:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}

A quick solution would be to call onBackPressed() instead of NavUtils.navigateUpFromSameTask(this). This, however, isn't recommended and you should try to implement proper navigation logic. Take a look at the answers to this question as well as these tips.

Related

Android UP button goes to mainActivity instead of current

I have 3 activities:
MainActivity (start activity with grid view)
FragmentActivity (full screen image slider accessed from grid view)
InfoActivity (blank activity opened from menu in either Main or Fragment)
When I go from MainActivity to InfoActivity:
startActivity(new Intent(MainActivity.this, InfoActivity.class));
and press the "up" button I get back to main activity.
When I go from FragmentActivity to InfoActivity
startActivity(new Intent(this, InfoActivity.class));
and press the "up" I STILL get back to MainActivity.
I know it's because my MainActivity is the parent of Info.
But how do I make the "up" behave like the "back" button, so that I can go from InfoActivity to FragmentActivity?
Any help will be much appreciated!
You have to override onOptionsItemSelected in the InfoActivity class to intercept the "up" button and call onBackPressed from there. Like so:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item != null && item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
The answer from #Firoze Rakib will work but I would like to provide you better understanding of the problem and solution advised by Google.
First of all you most probably have defined parent in your AndroidManifest.xml, you should remove it since your InfoActivity can have different parents.
Secondly, there is a small difference between BACK BUTTON(which is programatically called in #Firoze answer) and UP BUTTON. First one simply destroy current activity and show activity that was previously shown, theoretically it does not have to be any of your parents. The UP BUTTON starts new instance of the parent Activity and clear the activity stack in the current task.
If this difference matters for you then you should follow instructions from Android Developers page. Take a look at second point that starts with this sentence:
Or, override getSupportParentActivityIntent() and
onCreateSupportNavigateUpTaskStack() in your activity.
how do i make the "up" behave like the "back" button...
I believe another answer has the technical details if you are determined to do this (which you should not).
The purpose of my answer is to point out that making up behave like back will confuse users by violating the Android design guidelines.
Up navigation is an alternative to using the back key with a different purpose: it gives the user a way to navigate back to the top of an app when they are "lost" deep in a hierarchy and they might have to press the back key many times to escape. In this situation some users will panic and start mashing the back key, and up nav is meant to give them an alternative.
Try this
public boolean onOptionsItemSelected(MenuItem item) {
if(item != null )
if( && item.getItemId() == android.R.id.home) {
// your code
return true;
}
return super.onOptionsItemSelected(item);
}

How to handle BackPressed key of for Fragment by custom?

In my android App, I have used fragment ,But for handling back key I have got problem ,
My structure is,
Its user account structure, in that,
1)Main Wall=> In that sub fragment->another fragment -> another fragment
2)When I click one button (Friends wall button )which is on this screen ,
then, open another wall which is same like main wall .
3)From main wall you will go another next screen.
I have maintain Stack of fragment for taking back, when back key pressed. Its works for Main wall ,
But for back from friends wall to user wall its handling is goes difficult ,
Please give me any clue or suggestion for handling such condition.
,Now I handle B
this is the right way to do it using up button in the actionBar as in the screenshot below:
#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);
}

How to execute code when Home button is pressed?

I want to run code only when the home button (when the app is sent to background) is pressed. I tried using the lifecycle-method but the problem is that they also get executed when and other dialog/activity is started. I only want to check if the application is sent to background. How can I achieve that?
In onOptionsItemSelected(MenuItem item);, check to see if the item clicked is the home button:
EDIT:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Activity.onUserLeaveHint() fires on your activity when the Activity is going to be backgrounded as a result of user action, such as pressing the home button or switching apps.
Well since you are writing the code you can set some boolean flags when you start explicitly another acitivty, and check that when your activity goes through onPause()...
if they are false, it means someone else is pausing your activity.
Maybe not the most elegant solution but it will work if that is the only problem you have.
You could have a tracking system (basically a counter) to know when one of your activities is resumed and paused, thus allowing you to know if any of your activities is open.
This way you could know if your app is "open". but this would not apply only to the home button, but to the back button (last back to close your app) or even opening another app from the notification bar.
Android OS will kill your application to free resources, it wont stay in the background all the time. Use service if you want your app keep running in the background
If you want to do when the home button is pressed and the activity is going background, you can override this on the activity:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_HOME:
//somthing you want to do
default:
return super.onKeyUp(keyCode, event);
}
}
I am using the KeyUp event because that's when the app goes backgroud, the user can do a longPress and I don't think you want to do the method when the app still open.
this is sort of a cheat answer, but i find it to be much more reliable.
one method that's always called when you press the home button is the surfacedestroyed method for any surfaceviews contained in any of the program's activities.
Putting the code you want to execute there, having the method point to another method you want to execute, or having the method trigger a flag that points to the method you want to execute will do the job.

Android Back Stack - UP button method

I have Activity A (Main) and Activity B.
Fact: Activity A has: android:launchMode="singleInstance"
Usual scenario is:
User launches application > Activity A.
User clicks an item > Activity B
3.A. If user clicks on back/up buttons > Back to A (without calling finish() on B)
User clicks the SAME item as before > Forth to B.
At this point he can go back and forth without new instances. It's all in the stack and it doesn't recreate activities. (All right!)
3.B. If user clicks Home, then goes to task manager and brings the app to front > Activity B (all good, so far)
If user clicks UP button, it goes to TASK MANAGER, and I want it to go to Activity A (back button is expected to work this way, so let's focus on UP button).
Here's the implementation I have in Activity B for BACK and UP buttons.
#Override
public void onBackPressed() {
moveTaskToBack(true);
// I don't want to finish() the activity, so the user can reclick the same
// item without reloading the whole activity again (webview slow when parsing html).
return;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
moveTaskToBack(true);
// I don't want to finish() the activity... idem.
// I need to implement here the bring Activity A to front
break;
}
}
So, what I want is: to "Go Back" to Activity A keeping the same idea of using the stack to reload Activity B if needed, without using Intents (unless it calls activity to front, without adding items to the stack.)
Let me know if I explained myself clearly and if you need more info.
UPDATE:
I've found this at: http://developer.android.com/training/implementing-navigation/ancestral.html
This is how I adapted it.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent upIntent = new Intent(this, Activity_A.class);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent).startActivities();
} else {
moveTaskToBack(true); // I want it this way. Don't worry.
}
break;
}
}
But the method NavUtils.shouldUpRecreateTask is ALWAYS returning false.
I did the http://developer.android.com/training/implementing-navigation/ancestral.html#SpecifyParent part, so that's not the issue.
My problem is that I want to recognize if Activity A exists in the stack, for when i.e. the app is launched from the task manager.
How can I achieve this?
Thanks.
moveTaskToBack moves the entire task to the background. it doesn't finish the activity.
in order to have full control of activities, you have some possible solutions:
create your own global manager for the activities, monitor each of them through all of their lifecycle and decide what to do on each event.
you could also finish each activity as soon as you go from it, and put "it" (just its name or something) in a stack and restore its state when you come back to it.
use fragments instead, and manage them all on a single activity. be warned of configurations changes though.

Android activity is dimmed when returning with back button

I have a very simple scenario:
In an activity the menu key is pressed, taking us to a menu.
A menu option is selected taking us to an activity which presents a listview.
Pressing the backbutton on the listview activity returns us to the original activity but it instantly dims and won't take input without pressing the back button or menu button to wake it up.
Is there any way of returning to the original activity so that it still responds directly to input? (clearly there is a way because I've seen plenty of applications which manage to achieve this).
The code I'm using to respond to the menu item being selected is:
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_settings:
startActivity(new Intent(this, MenuList.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
When I use the back button to return to this activity from MenuList, this activiy dims and needs to be 'woken up' using e.g. the back button.
Many thanks
I guess your newly started activity hasn't finished yet, thats why you need to press back.
You can run the following in a shell, to find out what is going on.
adb logcat -b events | grep am

Categories

Resources