Android when i pressed back from toolbar first activity is refreshed - android

I have two activity in my app in first activity I have recylerview list, when user click on any item of recycler view second activity is called.
On second activity I have toolbar with back button
setSupportActionBar(mtoolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Therefore when user click on back button first activity is called but recycler view is reloaded again.
I don't want it. and if user press mobile back button that time first page is not reloaded.
So what I do to overcome from this problem any suggestion?
Regards
Manish

put finish() on onOptionsItemSelected, so when you click on back button the current activity will be closed and the old activity will be showed without reload again
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
...................................

Related

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

App home button not preserving data

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.

How to load the values for edittext fields when back button is pressed

I have three pages for user registration. I implemented this using intents.For each page I have next button to navigate to next page.I have to save all the details at the 3rd page when clicking the submit button.I have back button in action bar to go forth to the pages.Its all working fine. But now my problem is when I go back to the pages it shows empty fields.But I want to know how to make that fields to be filled.Can anyone help me out for this?
Just try with finish()
While moving from 1st activity to 2nd activity, don't call finish().In 2nd Activity while clicking back menu just call finish()
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.back_btn: //your back btn id
finish();
}
return true;
}

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