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);
}
});
Related
I have an activity in an Android application that includes both native UI elements and a WebView. The WebView is normally hidden (Visibility.GONE), and it performs occasional work "in the background" on its JavaScript loop. On a particular button click, the WebView is made visible and the user can see the web page. On a subsequent button click, the WebView is hidden again. The user can repeat this process.
I added a call to WebView.postVisualStateCallback when the user presses the button to make the WebView visible. I only make the WebView visible during the VisualStateCallback.onComplete. This works great the first time the user clicks the button and I make the WebView visible. However, after the user dismisses the WebView and I make it hidden again, I cannot repeat this process. Any subsequent call to WebView.postVisualStateCallback never results in another call to VisualStateCallback.onComplete.
mWebView.postVisualStateCallback(++mWebViewVisualStateCallback, new WebView.VisualStateCallback() {
#Override
public void onComplete(long requestId) {
if (requestId == mWebViewVisualStateCallback) {
mWebView.setVisibility(View.VISIBLE);
}
}
});
I am not navigating to new web pages in the WebView. The web page is essentially a long-lived single-page app. The DOM may be changing in the WebView while it is in the background, which is why I'm trying to subsequently call WebView.postVisualStateCallback. I am testing on an emulator and Pixel 2 with Oreo 8.1/API 27.
Is it possible to use WebView.postVisualStateCallback multiple times on the same main frame? Why would WebView.postVisualStateCallback only work once?
Quoted from android website, I think you might need to consider these situations when using postVisualStateCallback()
To guarantee that the WebView will successfully render the first frame
after the VisualStateCallback#onComplete method has been called a set
of conditions must be met:
If the WebView's visibility is set to VISIBLE then the WebView must be attached to the view hierarchy.
If the WebView's visibility is set to INVISIBLE then the WebView must be attached to the view hierarchy and must be made VISIBLE from
the VisualStateCallback#onComplete method.
If the WebView's visibility is set to GONE then the WebView must be attached to the view hierarchy and its LayoutParams's width and height
need to be set to fixed values and must be made VISIBLE from the
VisualStateCallback#onComplete method.
Hope this is helpful~
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().
I have an activity that loads content from the internet when it initially loads, however the order it takes is:
1) User hits button to go to new activity
2) App stays on old activity
3) App loads content from internet (usually takes about a second or two)
4) App brings user to new activity with content already loaded
Now this is fine, however it can be confusing for a user because while they are still on the original activity, there is no indication that their input did anything. If the user were to click the button again, it wouldn't register that as another click on the original activity, it would register that as a click on the new activity which is still loading, which may cause a user to end up somewhere they did not wish to be.
I'd like to be able to fix this by having the order of the process be:
1) User hits button to go to new activity
2) Old activity disappears, brings user to "blank" new activity without content loaded
3) Display a loading wheel
4) App loads content from internet
5) Content is displayed and loading wheel disappears.
However I can't figure out how to accomplish this, I placed all my code in the onResume() method because I thought that would work, but the content loaded the same way as it always has, does anyone have any suggestions on how to accomplish what I want? Here is the code in my onResume() method:
protected void onResume() {
super.onResume();
setContentView(R.layout.coupon);
//method call to access the URL needed to display the content
accessURL(Global.contentURL);
}
Any help would be greatly appreciated.
You can use an AsyncTask that creates a ProgressDialog while it fetches the data in the background. There are many questions about this on SO (e.g. How to use AsyncTask to show a ProgressDialog while doing background work in Android?) as well as on the Internet.
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.
I've trying for days now to get a solution to show a splash screen while loading a webview.
I've looked around and there are several approaches:
- make activity just to show splash screen, wait a few seconds and then start the webview activity -> this is not a solution since all the loading is again done after splash screen is closed
second approach is something like this:
myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
myWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
findViewById(R.id.webview).setVisibility(View.VISIBLE);
//hide loading image
findViewById(R.id.imageView1).setVisibility(View.GONE);
//show webview
}
});
This works, but again there is a slight appearance of a white screen while transitioning to webview. And also you can't control the timing of your splash screen.
third solution is something that I'd like to make, but (even after few days of searching and trying) don't know how -> show splash screen in one thread, prepare everything for a webview in another and then simply switch to webview.
My question is, can anybody show me where to start with this third solution?
I wanted to try something like this! but I couldn't figure out how to pass webview to another activity and from what I read, this is not even a good idea.
Any suggestion, ideas, pointers? Is there a way to show splash screen, prepare(inflate) webview in another thread and then switch to it after loading url (and everything else) is done?
Thanks!!!
Try to use a WebChromeClient on the Webview. In the WebChromeClient, you have a method onProgressChange.
You can do disappear the splash screen when the onProgressChange tell you that the page are finish to load.
Edit:
You can call in a thread your Url with HttpGet for example and retrieve the response. When you have the response you can load the webview with the loadData(...) method.