Back button between fragments - android

I'm currently working on a project that has an activity which is consisted of two fragments.
The first fragment shows a custom expandable list. Every row is created from a custom layout that has a checkbox in the right side of it.
The second fragment shows more details about the clicked row from the list. In order to open the second fragment, the user has to click on the row. The checkbox is used for another reason.
So, what I'm trying to do is to display these two fragments side by side only when the application runs in tablets. When the app runs in handsets and the user presses one row, the second fragment should be displayed on top.
Furthermore, I have an action bar at the top of the screen which has implemented the usual back button.
The problem exists when I open the second fragment when I have already selected some checkboxes. When I press the back button, which navigates me to the first fragment, the checkboxes will not be checked.
The onSaveInstanceSate method is obviously not called (as the parent activity is not getting paused), so I can't save the ArrayList that stores the checked rows.
Last but not least, the fragments are being added dynamically.
The question
How can I properly implement the back button so when the user uses a
tablet, the back button should be used in order to close the activity, or a
handset, so the back button should be used as a navigation back to the first fragment with the ability to restore it's previous state?

if (mFragmentManager.getBackStackEntryCount() == 0) {
LogUtil.d(TAG,
"home fragment" + mFragmentManager.getBackStackEntryCount());
this.finish();
} else {
mFragmentManager.popBackStackImmediate();
}
try this should work , happy coding

My first idea would be to create a boolean in the resources of your project: in the "values" directory, your boolean would be false, for example, and in your "values-sw600dp" and "values-sw720dp-land" directory, the boolean would be true.
Then, in your code, you would check the boolean (using R.boolean.your_boolean) to know if this is a tablet or a handset.
Then, with a simple if/else, you would implement your code, depending on the value of your boolean...
if(yourBoolean){
//We are on a tablet
finish();
}else{
//We are on a handset
//Your code to navigate back...
}

You need to (1) detect if the user is on a tablet and (2) control the back function accordingly. I'm not sure how you're currently detecting whether the device is a tablet but a very easy method is described here. It involves a boolean resource that you can access when customizing your back function to determine the device type.
What I would do is override onBackPressed in your hosting Activity and control back function from there
#Override
public void onBackPressed(){
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize){
moveTaskToBack(true);
} else {
//handle fragment back stack
}
}
The info on handling the back stack and replacing the fragment is here in the android docs. I will update that section later but I have to run for now.

Related

Going back to fragment recreates and adds to existing dataset

I have a Fragment and I create some data inside onCreate(). From this fragment I can go to another one by clicking a Button.
The problem starts when I click the back button. What happens then is that it goes again through onCreate() and re-creates a new dataset and adds it to the old one.
The result is that I end up with two datasets instead of one. How can I skip onCreate when I'm coming from back button or is there another way ?
#Override
public void onBackPressed() {
//Check if you're in the button pressed-fragment
if(){
//You're code here
}
}
This overrides the backButton and you can do what you want - create new fragments or load the from backstack and other stuff. Be careful though, people don't like to not being able to quit the app with back button.

Manually Access Android Back Stack

I'm having problems coming up with a solution to this problem.
Basically I have a load of tabs in my ActionBar. When each is touched the fragments from the previous tab are detached and the fragments for the new tab are added using replace (if they haven't been instantiated yet) or attached (if they have). I think I got this method from Google and it was working fine until now.
Example of adding a tab's fragments:
if(tab.getText().equals(context.getString(R.string.title_class_tab))) {
if(browser == null) {
browser = CourseBrowserFragment.newInstance(false);
fragmentTransaction.replace(leftContainerId, browser);
} else {
fragmentTransaction.attach(browser);
}
if(lessonViewer == null) {
lessonViewer = LessonViewerFragment.newInstance(false);
fragmentTransaction.replace(rightContainerId, lessonViewer);
} else {
fragmentTransaction.attach(lessonViewer);
}
}
and removing:
if(tab.getText().equals(context.getString(R.string.title_class_tab))) {
if(browser != null) {
fragmentTransaction.detach(browser);
}
if(lessonViewer != null) {
fragmentTransaction.detach(lessonViewer);
}
}
The problem arises from the layout I need for one of the tabs. Basically it's like the Gmail app. There are two fragments (let's say Panel A and Panel B) and when you push a button Panel A slides out, Panel B slides to Panel A's old position and a new, third one (Panel C) slides in from the right.
I had this working fine but now I've added the sliding-in FragmentTransaction to the back stack so that the user can touch the back button and Panel C will slide back out and Panel A will come back. Again, like Gmail.
Except when the user goes to a different tab this transaction is still on the back stack and executes if the user presses back. The fragments end up in crazy places. What I need to do is remove it from the back stack when the user navigates to a different tab. Is there any way I can do this? FragmentManager doesn't seem to let you manually remove things from the back stack and using the popBackStack() method doesn't just remove the transaction, it executes it. I want to remove it when the user navigates away and put it back when the user returns.
I think I can get a hold of the "Back Stack Entry" for this transaction using "getBackStackEntryAt" but it's not much good if I can't remove it and put it back in place when the user comes back to the tab.
The only possible solution I can think of is not using the back stack and overriding onBackButtonPressed instead. From there I could just do a reverse of the transaction if necessary.
Thanks for any help and sorry if I'm being incoherent.
Not sure if this would qualify as a solution but I ended up just not adding the transaction to the back stack and just doing a fresh transaction when the user swiped or pressed back. The transaction just did the reverse of the original one with animations etc.
The way I managed the back button is I set a boolean to true if I was in the layout showing Panel C. If the user swipes back into the Panel A layout or navigates away the boolean is set to false. I then overrode the onBackButtonPressed method in the Activity and if the boolean was true (ie: we're in the Panel C layout) I run that reverse transaction otherwise I just call super.onBackButtonPressed() (ie: perform standard back button behaviour).

When i Click Back Button in android i want to make changes in previous activity?

When I click back button in my app I want to change some data in previous activity.
public void onBackPressed() {
super.onBackPressed();
Log.v("Back Button ","Pressed");
}
I am trying to using this but cant help it out ?
do I need to maintain backstack of activities which back button internally does?pls help
i want to change menubar images (wriiten by me bottom of every activty in my app) item which is present in every activty but when I click menubar (clickable) it is not able to change menubar images as per activity changes.
You want to look at startActivityForResult. The previous parent Activity should spawn the next one with this call. Data can be passed backwards via extras (Bundle) in an Intent when returning to the parent Activity in onActivityResult - or you can simply use the result codes.
You can find more information here: http://developer.android.com/reference/android/app/Activity.html#StartingActivities
Better use with onKeyDown. & onKeyDown Example
Just override this android's default method. It'll provide the changes with whatever you want.

back button return the application to inconsistent state

Today I was testing my app (wich is a very simple note application) but some strange behaviour occur .
The app consists of a big ListView which displays all the notes and a button to make a new note (activity A).
When I press the button to "Add new note",it takes me to the activity B
(with all the fields like title and text).
When I press "Add" ,it take me back to the activity A and displays ok
the new Note .
The thing is that I started pressing BACK and it began to take me "back in time" where some notes were still not created,until it reached the time they were 0 notes .
Since I am new to Android devel ,is this normal or is it something wrong with my app ?
When you go from one activity to another then you should finish your activity and then start new activity.
Try the below code:
finish();
startActivity(new Intent(CurrentActivity.this,NewActivity.class));
You probably need to implement some sort of refresh method to update your ListView. Depending on how you are persisting the data, you could create a method that retrieves the latest data and then updates the adapter for the ListView. Once you have that implemented, you should call the method in onResume().

android Activity Group

Today i met with the serious problem. Actually i have 5 activities in my activity group.
from 1st activity i go to 2nd and from 2nd i go to 3rd and so on....
and on the 5th screen when i press back key i came to 4th and so on....
When i again go to the same process it displays me the previous displayed data as well on the screen. m not able to find the sollution for the same.
i need that every time i follow the process it will show me the new data not with the previous one data.
pls help me.
i cant paste the whole code. its 5 activities.
I don't think you mean "activity group," which is something else entirely. I assume you're talking about the task stack. What you probably want to do is override the onRestart method on your activity, setting it to call whatever method it is that you use to refresh your view's underlying data. You may want to do that in conjunction with a member variable "dirty" boolean flag you set on your activity before you start another one so that it only triggers if you're coming back from another activity (and not when the user switches to another app and back to yours).
You can override back button of Android .
In that method you can clear all views from stack and set first view on screen.
Below is Code from my app this you can use as per your requirement.
#Override
public void onBackPressed() {
super.onBackPressed();
Tab2GroupActivity.group.back();
}
In Tab2GroupActivity class
public void back() {
if (history.size() > 0) {
history.remove(history.size() - 1);
setContentView(history.get(history.size() - 1));
} else {
finish();
}
}
Here history is my list which contains all opened views in it.
this solved my issue.

Categories

Resources