How to handle BackPressed key of for Fragment by custom? - android

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

Related

Android when i pressed back from toolbar first activity is refreshed

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;
...................................

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

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 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