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.
Related
When iam moving current activity to list activity in list activity have 8 items and in list activity have add button widget when I added one item using add button so total 9 items in list activity. Here my problem is when I click on back button my application not moving to previous activity showing list activity with 8 items after click again back button moving previous activity how to solve this issue without using intent
override onBackPressed() method the way you want.
When you use realtime database like Firebase at the moment you will have updated list. I think when you click on button you are opening new activity and don't finish last one. So you come back to last view again. Of course if you send your code, you will get proper answer.
You should override method onBackPressed() in the activity where your back button is not working correctly for you:
#Override
public void onBackPressed()
{
finish();
super.onBackPressed();
}
I think that adding method call finish() should solve your problem.
I am calling a Master/Detail activity in android by clicking on a button placed in another activity (UserActivity). The strange thing is, that if i click the back button in the Master/Detail activity, I loose the Data in the state of the UserActivity. It wants to execute the onCreate Method again.
If I click on the login-button in the LoginActivity where i am redirected to the UserActivitiy and i go back with the Back-Button of the "Smartphone", the username and password i typed are still there. So there i do not loose the data.
Is there a difference between the back-button of the Smartphone and the back-button at the top of the program? I am a bit confused now and i know how to persist the state of the Activity. But my question is, why i am having this behavior on the one side and on the other not.
Just in case you will leave the question like this and don't add code:
What will probably help is checking for the amount of items that are already there. When I ran into this issue, the onStart() got called so quickly that it seemed to me that the Activity has lost the data. Actually it DID have the data, but calling onCreate/onStart (I'm in a Fragment) NULLed it.
What I did to avoid this is to check if there is a need to load items in the list. If there is, it calls a method that contains what the old onCreate/onStart did. If there is no need to load data, it will just skip the step and live with the "old" data happily for the rest of its life.
#Override
public void onStart() {
super.onStart();
if(DummyContent.ITEMS.size()<3){
initializeApp();
}
}
void initializeApp(){
videoTitles=new ArrayList<String>();
videoUrls=new ArrayList<String>();
. . .
}
I have an application with two activities. The first contains a tab bar with three fragments in it. Clicking on a button in the fragment loads the second activity.
When the second activity is dismissed, either by the back button or by the Up Navigation, I need to refresh the data in two list views in the main activity's fragments.
What is the best way to do this?
The key is understanding the Activity and Fragment lifecycles. When the first Activity and Fragment return from to the foreground several events are called such as onResume() this is one spot your refresh could occur at.
You need to start your second Activity with startActivityForResult, and in onActivityResult, refresh your fragments.
When your second activity dismissed, the first one brought to front, right? This causes one of the tab fragment to show again. In your fragment you probably have a List Adapters for your list views.
Try this in your fragment:
#Override
public void onResume() {
super.onResume();
mListAdapter.notifyDataSetChanged();
}
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.
I have a Main Activity which a FragmentActivity and I have three fragments which are on three separate tabs. On fragment two, I have a button which opens an an activity on which the user can click OK or cancel. Right now when he clicks OK or cancel nothing happens and it just quits the application.
What I would like it to do, is that in onClick() (of either the OK or cancel buttons) of the activity that was opened from the fragment, it goes back to the original activity and back to the same fragment it was on, in this case fragment two.
I right now I have it so that it goes back to the original activity but the first fragment, not the one from which the button was pressed...how can I accomplish this? Here is what I have to go back to the original activity:
btnOK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//execute background task for processing
new SaveDetails().execute(); //this actually calls an HTTPRequest
Intent a = new Intent(ACTIVITY_Edit_Trip.this, ACTIVITY_MAIN_CONTROLLER.class);
startActivity(a);
}
});
The tabs are working fine and i can swipe between them, I just don't know how to go back to a specific one from an activity. Is this possible?
Thanks for the help! If I need to post my PagerAdapter code, I can do that, just let me know!
Edit: I have two activities, the main activity and an edit activity. My main activity is a FragmentActivity which is also the one that creates the tabs where the fragments live one. The edit activity is accessed only when the user presses a button from fragment 2.
Once the button on fragment two is pressed, the edit activity opens and it has two buttons OK and cancel, if the user presses either it should go back to the main activity > fragment 2. Right now with the code I have posted, it goes to main activity > fragment 1.