Android - how do I show a startup screen to cover initial processing? - android

I have an Android app that does about 30 seconds of heavy duty number crunching at the start of the app. How do I display a screen that disappears at the end of the initial processing?
What I currently do is:
My activity_main.xml's topmost view is a ViewAnimator which has (for now) two child views - the main view, and a FrameLayout which currently is a white background that fills the screen and a child text view with "Processing..." on it.
My MainActivity.java's onCreate() method calls a user-defined method doInitialProcessing(); doInitialProcessing calls the ViewAnimator's setDisplayedChild method to show the "Processing" view, then does the number crunching, then calls setDisplayedChild again to show the main view.
In the Eclipse Emulator, what usually happens is, the screen turns white at first, then goes black, then the main view appears. (Every now and then, the "Processing..." text shows up.)
Is this the right way to go about this? Show the initial call to setDisplayedChild be in a separate thread? Does the fact that it is called from onCreate have anything to do with it?

There are two ways to do it:
Create a normal activity with a layout for your splash and start the other activity using a timer. (this way, you cannot load data in the second activity)
The other way is to create a splash fragment, that is displayed for a few seconds while the content is loaded in the background.
I use the following timer to start my activity from SPlash activity. This can also be used in a fragment:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent in = new Intent(SplashActivity.this, HomeActivity.class);
startActivity(in);
finish();
}
}, SPLASH_TIME_OUT);
SPLASH_TIME_OUT is the delay in integer value. (1000 for sec)

You can use a Splash Screen that shows something like the logo of your app for instance for some seconds while the app munches on the numbers.Here is a good tutorial on creating Splash Screens.You can also read up on this StackOverflow question.

Related

Android shared element transition from splash screen to main activity

My application displays a splash screen for 1 second before displaying the main activity. Both the splash screen and main activity share a common image that is required to animate from the center of the splash screen to the top of the main activity layout.
Since it wasn't obvious how to accomplish this animation if the splash screen was implemented as a <layer-list> background image in the main activity (see Splash Screens the Right Way or How do I make a splash screen?), I decided to implement the splash screen as a normal activity and use a shared element transition to animate the image between the two activities. Initially, I used the following onCreate() implementation in the splash activity:
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
ImageView imageView = (ImageView)findViewById(R.id.imageView);
String transitionName = ViewCompat.getTransitionName(imageView);
Intent intent = new Intent(this, LoginActivity.class);
ActivityOptionsCompat options =
ActivityOptionsCompat.makeSceneTransitionAnimation(
this, imageView, transitionName);
imageView.postDelayed(() -> {
ActivityCompat.startActivity(SplashActivity.this, intent, options.toBundle());
finish();
}, 1000);
}
}
There are two problems with this approach:
Calling finish() immediately after calling startActivity() causes the splash activity window to be hidden/destroyed before the animation starts which results in the home screen temporarily flashing into view during the animation.
Pressing back from the main activity automatically triggers a shared element return transition that results in the image appearing suspended over the home screen for 500 ms after the main activity window has been closed. The return transition fails because the splash activity has already called finish() and therefore is no longer on the back stack.
To solve the first problem, I wrapped the finish() call in a postDelay() Runnable to ensure that it only gets invoked once the shared element transition has completed. A 1500 ms delay works in my application, but that value should be adjusted to depending on the timing required by other use cases.
...
imageView.postDelayed(() -> {
ActivityCompat.startActivity(SplashActivity.this, intent, options.toBundle());
imageView.postDelayed(this::finish, 1500);
}, 1000);
To solve the second problem, I overrode the main activity's onBackPressed() method to directly call finish() thereby avoiding the default implementation's call to finishAfterTransitions(). This prevents the Activity from attempting to perform the shared element return transition.
#Override
public void onBackPressed() {
finish();
}
Any alternative approaches or suggestions to improve this solution would be appreciated.
Why don't you use fragments that sharing same activity instead? I don't see a reason to use 2 different activities for such a simple things.
Instead of finish(); use ActivityCompat. finishAfterTransition(this);
I faced similar problem also.
One approach can be to not to call finish() in Splash screen and in next screen call finishAffinity() on onBackPressed().

Android onWindowFocusChanged called to early

In my activity needs to download an image from a webservice, therefore I wrote an updateUi method which get called in onCreate() to hide the ui elements (because of missing data at this time) and to show a simple TextView with a message like "data loading...".
Then I use the onWindowFocusChanged with a loaded boolean to call it only one time. In the onWindowFocusChanged I download all data I need from the webservice and call updateUi again to show the original ui and hide the "data loading" TextView.
The goal is to immediately open the new activity after the user pressed a button instead of hidden processing while the user don't know what's happening.
I have like 10 other activities where this method works very well without any trouble, but in this particular activity it seems like onWindowFocusChanged get called before the new activity shows up. If I click on the button to open the activity nothing happens on the display for some secs and then the activity starts with the original ui with all data filled in.
I don't understand why the activity doesn't show up with the "data loading.." TextView instead of doing the tasks in onWindowFocusChanged while the activity didn't show up already?
The code is the same like in my other activities, does someone have any experience with this issue?
I have noticed, that the problem doesnt exists in the first creation of the activity. if i click on a button which start the activity the first time the textview with the loading message is showing and the other ui elements are hidden.
if i leave the activity an start it again the problem occurs...

How to show loading message or dialog until the other activity loading because it take long time -android-

i have activity take long time until get loading so i want when i click start this activity to show loading message like ( loading..... )
here is the button that started this activity
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(test.this, MainActivity.class);
startActivity(intent);
}
});
so what the code that should i write here to show this loading message until the MainActivity load?
If you have activity A that on click shows activity B that takes long time to load, the most common way is this:
in the activity B layout view have a progress bar and make it visible in onCreate of activity B
all long running operations put either in a asyncTask or in some Service that would run in the background
when long running operation finishes - update progress bar by setting it's visibility to View.GONE.
I would write you a code, but since I don't have your code, I think the best way I could help is to break your problem into a steps.
daneejela provided a solution but I do think that the reason for you long startup of your MainActivity is the real problem and should get fixed.
Activities will take long to startup when you do too much block method calls or such things in the MainThread.
Try to do those things in a background Thread (you can use AsyncTask). This will result in an immediate Activity startup. While your AsyncTask is working, you can show a little ProgressDialog or better, show somewhere in your Activity a ProgressBar which is not blocking the user from doing any kind of action.
You should definitly understand Android's Main- and BackgroundThreading. There are also great tutorials for Dialogs. You should also check out the Android Design Guidlines.

Smooth transition between activities without black screen

I use activity with ImageView, and by click on button it switches to activity with VideoWiew and plays movie, the movies first frame is the image on previous activity. I disabled animation between activity by using
Intent intent = new Intent(ImageClass.this, MovieClass.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
but it still flashes black screen between them, how can i disable this?
One of the simplest way is to move all the expensive (time-consuming) processing from your activity's onCreate and onStart method to onResume mthod. By this, your newly launched activity will be visible right after its launched but then will take a little extra to make it available for user to interact. Further, I would suggest you to move all the heavy lifting in AsyncTask for smoother UI experience.
I recently had a problem with smooth transition. Even though I am loading the lengthy tasks in AsyncTask, it still takes time to load, and the screen has to wait.
If you have a WebView, for example, that takes a while to load, and under the WebView, you have some TextViews and other things, supposedly a layout.
The layout will load first, and the WebView later, making the screen loading ackward.
A solution for this would be to set the Visibility to GONE on all the objects before it loads, and then set to VISIBLE after the WebView loads.
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
webView.setVisibility(View.VISIBLE);
LinearLayout ll_load_after_web_view_bottom = findViewById(R.id.ll_load_after_web_view_bottom);
LinearLayout ll_load_after_web_view_top = findViewById(R.id.ll_load_after_web_view_top);
ll_load_after_web_view_bottom.setVisibility(View.VISIBLE);
ll_load_after_web_view_top.setVisibility(View.VISIBLE);
}
});

android splash screen

i have a program that shows a splash screen.But the problem is whenever i refresh same page again it appears.Is there any method to stop splash screen again and again.I want it just comes at first time not again and again.
Thank you
So you basically want the splash screen appear once per app launch.
Here's a quick and dirty way:
Subclass android.app.Application as, say, MyApp;
Declare that class in AndroidManifest.xml (<application android:name=".MyApp" ... >) so that it would get instantiated at app launch time;
Give it a public static boolean SPLASH_SHOWN = false;
Now, in your Activity's onCreate() check if SPLASH_SHOWN = false, show splash and set it to true.
If you implement the splash screen in the same activity with another view layout (the main layout) - for example, display splash screen first, then swich the view to the main layout - I suggest to control the content view setting in onResume().
Use a boolean variable (displayedSplash) to remember whether the splash screen has been displayed. If it has not been displayed (start the activity the first time, displayedSplash == false), then set the view to the splash screen, after that switch to the main layout and set displayedSplash = true. If it has been displayed (refresh the page, displayedSplash == true), just set the view to the main layout.
Another simple and safe solution for simple splash screen is implementing it as an activity, then start the main activity. When the onResume() is called the second time, just finish the activity. Please refer to this Wiki for the detail.
Most of the times I call finish() method inside onPause() method in splash activity. This will work fine for me

Categories

Resources