Android loading activity in splashscreen - java

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.

Related

Is there any method that i can call on SecondActivity to add a new item on Arraylist on FirstActivity.Fragment that has Recyclerview?

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

Hide MainActiviy when DialogActivity starts

I use firebird cloud messaging to receive data in the background, then a dialog (new Activity with Theme.AppCompat.Dialog as style) will appear. My problem is that the surface of the MainActivity is displayed every time in the background. Is it possible to hide MainActivity without terminating it? Someone has perhaps an idea?
When i set in the Manifest at the MainActivity the value
android:noHistory="true"
i looks good, but the notification is then in the app history. this looks bad
I mean when the activity is in Background that the MainActivity is still visible
As you are using an Activity as a dialog, your MainActivity remains in the Activity Stack in a paused state. However, even if you make the Dialog Activity full-screen, it is not guaranteed that MainActivity will be kept alive. The OS can kill the activity anytime if it needs memory for other processes. This is stated on the Activity reference.
Instead you could try one of these alternative solutions:
Use a DialogFragment instead of a new Activity
Use regular Fragments, adding the fragment at runtime, allowing the user to switch between the dialog Fragment and the Main fragment.
Use a FrameLayout, to show/hide the dialog UI inside the same Main Activity layout.
I think are looking for this method
/* When {#link #FLAG_DIM_BEHIND} is set, this is the amount of dimming
* to apply. Range is from 1.0 for completely opaque to 0.0 for no
* dim.
*/
dialog.getWindow().getAttributes().dimAmount = 1f;

Batch edit activities in Android Studio

I have recently decided to add a Navigation Drawer to every screen in my app. I already have the working code. Is there a faster way to add the same layout to every screen or must I do this manually?
One way you could do it is to create an Activity or Fragment class say BaseActivity that all your Activity or Fragment will inherit, and put your Navigation Drawer logic there.

Async finish first then mainActivity starts

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.

Android - Show last viewed Activity When press (Home Button-->App Shortcut)

I came to do the eternal question, which so far have not found a solution, I searched on the internet the same problem but found a final solution to this problem.
when I have 2 activities open and I pull the 'Home Button' and then press the shortcut for my application, it shows me again the first activity (the launcher activity), and then to return to the activity that was displayed, I have to press the back button.
what is the solution to this problem?
I want to press the shortcut of my application (after having left my application by pressing the Home Button) show me the last activity was displayed, instead it shows me the first activity (activity launcher).
Thanks in advance.
That is the expected behavior. The launcher will launch the Activity with the filter android.intent.action.MAIN.
There are ways to work around it, though. A very simple one is to have a boolean flag mRunning that you will set to true upon launch. If true, then on the onStart() method you start an intent to launch your second Activity; if false, then go on with setContentView().
If you have several activities to go back to, then a feasible approach is to save the current activity in SharedPreferences and launch it the same way.
Alternatively, your main Activity may be just an entry Activity whose only job is to start the last Activity used.
EDIT: I found this duplicate question: How to make an android app return to the last open activity when relaunched? that has a much better ansewr than mine.
Your application is still running in the background when you press the home button. finish() the activity when you press the Home button if you want to go back.
Depending on whether or not your main activity is ever launched by another activity, or only the application icon, you can use a much simpler solution. If your main activity is only launched by the application icon, you can use isTaskRoot() to check if your main activity is being launched as a fresh start to the application, or if the user is returning and the main activity is being laid on top of other activities that you wish to display instead.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (! isTaskRoot()) {
finish();
} else {
...
}
}
You can use startActivityForResult replace for startActivity when you want to open another Activity.

Categories

Resources