I got a problem I need to solve to be able to parse my menu items in my navigation drawer.
Everything works fine: my asyncTask to get my xml feed of the internet and parse it into variables but, I want to use variable as menu items for my navigation drawer. My navigation drawer is called in my MainActivity its onCreate so that will be executed as first at the same time the asyncTask is running.
My question to this is: is there a way to finish the asyncTask first and then execute my Mainactivity its lifecycle
You can create some kind of SplashActivity during which you will download the list from the web and on its onPostExecute you can launch your main activity, passing the data you got with the Intent.
Related
Hi im currently building my First android app and i have a problem that i cant solve this past 3 days. so i have MainActivity and on it there is a Fragment with Recyclerview and ArrayList< Item > listOfItem, on this Fragment there is a Floating button that when i click it will take me to SecondActivity there is a edit text on this SecondaryActivity that i have to fill then i will pass the data back to listOfItem.My problem is what method can i call on SecondActivity to create/add an listOfItem when i go back to MainActivity Fragment? I dont want to make Adapter and listOfItem to be static. Is there a way? thanks
Simple, You can achieve this by using Shared Preference.
What you need to do is just save that Edit Text data to Model(POJO), save that Model to Preference and in your Main Activity - Fragment on Resume method show updated data from that Preference.
Yeah here you need to take care of
The fragments onResume() or onPause() will be called only when the Activities onResume() or onPause() is called. They are tightly coupled to the Activity.
For more convient and efficient handling of data from any activity or fragement to any activity or fragment, you can use Event Bus.. Here is link where you will get the details information: https://github.com/greenrobot/EventBus
The best solution would be to start the second Activity for result.
startActivityForResult(ActivityBIntent)
Then checking the result overriding
onActivityResult() on the first activity and updating your list/adapter from it
I am working with fragments and a navigation drawer. I have two fragments and a fragment manager to recycle them when I click on the menu items in the navigation drawer. I am also receiving data from a server in one of the fragments.
However, every time I reinitialize the fragment, the call to the server occurs again. I would like to know if there is any way that I can make sure that the server call only occurs in the beginning and when I click the refresh button.
I would like to know if there is any way that I can make sure that the
server call only occurs in the beginning and when I click the refresh
button.
Yes , when you click on the refresh button use an ClickListener to trigger the call.
If you want the call to occur only when fragment is created you can refer to the fragment life cycle. The better way is to put this call in the onCreate() method.
However if you don't want to make a call each time the fragment is created i recommend you tu use the SharedPreference to save the information that call have already been made.
exemple:
prefs = PreferenceManager.getDefaultSharedPreferences(this);
// The seconde value of method getBoolean incates the default value if constant not found
if (prefs.getBoolean(Constants.CALL_DONE, false)) {
// Make call
prefs.edit().putBoolean(Constants.CALL_DONE, true).apply();
}
The basic navigation of my app is a tab bar within one activity in which each tab is its own fragment. I am trying to get the back button to work to go back to the last tab it was on. I can get the back button to work/appear if the activities are going to the first tab by using this in the java class of the activity:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and this in the AndroidManifest.xml:
<activity android:name=".CreateNewPost" android:parentActivityName=".MainActivity"></activity>
This works fine, however when I go to a new activity from a button within the other tabs, if I use this same method it goes to tab1. How do I tell it to go to a specific tab or the last active fragment?
Any suggestions would help!
When you launch a new activity the old one either paused or stopped , as in either OnPause() or OnStop() method has been called. Then when you navigate back to a previous activity it is either started (OnStart()) or resuemd (OnResume()). And sometime the activity is destroyed and created again when navigating back to it using OnDestroy() and OnCreate() respectively.
The most guaranteed way that I can think of is to store the state of the first activity somewhere persistently and when the user navigates back to it check what has been stored and show that fragment.
You can use SharedPreferences to store this kind of data, see the google developer documentation on that here. Or onSaveInstanceState(Bundle savedInstanceState) check out this SO question on saving activity state. Although from the answers it is clear that onSaveInstanceState should not work in your specific case, but you should look at it any way because it is useful in other cases like changing screen orientation for example.
I have a splashscreen which downloads some files from my webserver.
After downloading I need to start the mainactivity which has to create many buttons at runtime and this needs some time.
Is it possible to start the mainactivity without showing it (so still show the splashscreen) and let the mainactivity its #onCreate() and after it is finished then show the mainactivity?
I think changing setContentView may not work because the buttons need the root layout of the mainactivity.
Unfortunately, NO.
onCreate only call when your activity start.
I suggest you to use only one activity with two fragments ( Splash fragment and Main fragment ).
First, you start activity and show splash fragment. When splash fragment finish, you show the main fragment.
It's faster than 2 activity, I'm sure that.
In my application, I have the Activity and several Fragments (Activity works as Controller and Fragments - as views)
In some of Fragments I need to show AlertDialogs and ProgressDialogs, Activity can change current Fragment.
My problem is: activity can receive broadcasts and C2DM notifications, and when I created AlertDialog, Activity can change fragment, but Dialog stays. So when user clicks on some buttons, app crashes.
DIalogFragments works like a simple Dialog.
Have I dismiss dialog manually or check if fragment is active? Is there any built-in tools?
First of all I might be missing something, no code etc to go by but...
Secondly: maybe you shouldn't be using dialoges? Seems like a cumbersome user interface. Just use fragments for those as well? Though you say you are using DialogFragments so maybe you've already thought about that and use them as "regular" fragments already.
Thirdly: Dismiss the dialogs when the fragment that showed is removed/hidden then? Use the onStop() callback for example in the fragment or a more central place where you are perhaps saving the currently showing fragment and deciding to display a new one.
Dismiss the dialog by calling ´dismiss´ on the Dialog object or Fragment or dismissDialog in the Activity.
See dismissing dialog: http://developer.android.com/guide/topics/ui/dialogs.html#DismissingADialog
You can still call dismiss on those DialogFragments.
http://developer.android.com/reference/android/app/DialogFragment.htm