Loading data only once in Android with ViewModel - java

I have an Android app with a main fragment that you can navigate from using navigation graph. Data is loaded when the main fragment is opened. The problem is when I navigate from it and go back the data is loaded again, because fragment is recreated. How is it possible to load data only once?

You can use ViewModel with the app and make a check if the ViewModel is already created do not load the data again

I suggest to you use SingleLiveEvent it will fix your problem please check this
Medium
StackOverFlow

Related

Save states of multiple activities - Android Application

I have read many posts about how to save a state of an activity using ViewModel and onSaveInstanceState(), however, I couldn't find something that explains the possibility of saving multiple activities states to get back to them using an "Edit" button (to edit some application information). Is this doable? Can someone enlighten me?
Thanks

Use .getWindow() in Application-file

This may be a silly question but I have the following situation: I want to setup my window layout every time the app is open, for example changing the status bar color. So I created my Application file because I heard it is better if you check something like this in the application file and not in the MainActivity.
Problem: How can I call the getWindow() method without an open activity.
Thanks for helping.
A bit tricky, but you can use Window manager in background-service to display your views without opening an Activity.
Also, for API Level 26+ (Oreo or above) you have to start your service as foreground service

How to close old activity before opening next?

I am building an Android app and are using the Parse SDK for Android.
Currently I am subscribing to channels with this in my main activity:
PushService.subscribe(this, "User_1_channel", MainActivity.class")
This works fine except for one thing. It opens up MainActivity and it gets put upon the other MainActivity in the stack which is very bad. I really want to remove the old activity before the new launches. I know I can do it with this but I have no idea where to put it since the Parse SDK handles everything.
launch.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
People say an alternative is to use a Custom broadcastreceiver. Problem with that approach is I have no idea how to subscribe to a channel without specifying an activity as the third parameter.
If you define the activity as SingleTask in your configuration file, it will indicate only that one activity will be used the Task.
android:launchMode="singleTask"

Fragment transition?

Any help would be great with my issue. Basically I'm using this tutorial and its a web view fragment. So I got rid of the WebView and made new activities. My issue is connection the activities with the drawer so then every thing goes to a different activity. Once again, any idea on how to do this would be helpful.
I would recommend this tutorial instead.
http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
I've used it and I have a functioning application at the moment. You can download the source, change the text and images if you'd like.

First android app for iOS developer

i am iOS app developer. Now when i created an app on iOS i want to do the same on android. With java i was familiar just now need some time to remember.
i was looking at some tutorial how to do one or other things. But what i can't find is the basics how everything works. How classes is interacting with each other.
For example i want to create registration window with few buttons and alerts.
I want registration window to be called just once when app is installed and just that.
Should i create new java class and few layouts, one for View with buttons and other for Alerts ?
Or should i create other class for alerts if i need them in other flow of my app ?
And how i should call that window with registration from my main class, which is
extends Activity
Also if there are some developers who came this road from objective-c (iOS) to java (android). It would be nice for some share experience how they did that.
Thank you for any help :)
Very few of the concepts in iOS and Android are similar. On Android you have Activites, Intendts the Manifest. When you design your layout it should be resolution independent. You have a search, back and a menu button and variable hardware. All of this has no equivalent in iOS.
That said, I think you just have to read the basic concepts and the getting started guide no matter if you come from iOS or never have done mobile development before.
EDIT
To answer your concrete question. Take a look at the lifecycle of an Activity and Preferences. With this, you could do some action on the first start of your main Activity and store some flag in the preferences when it's done. On the next start you just test that preference and skip the logic.
You can create one activity (.java file) and one layout(.xml file with buttons and input boxes) , alerts could be toast notifications:
http://developer.android.com/guide/topics/ui/notifiers/toasts.html
All you require for this is a activity and a layout xml for that activity, this activity will be your main ie the entry point to your application, in that activity oncreate method you can check if it is registered or not by setting a flag or something which will direct to the next activity if its registered.
GOOD LUCK...
Just like your nibs in iPhone you create xml layouts in Android. And for view controllers here you make activity. One important thing is AndroidManifest.xml file, it contains all information of your app (like plist) plus all the activity information(Intent type and launcher methods).

Categories