0

On MainActiivity, checking session stored and redirects to SecondActivity. What i expected is show only SecondActivity for session stored user but getting white screen for few second and redirect to SecondActivity. My onCreate method as follow

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        session= new Session(MainActivity.this);
        if(session.getData("auth").equals("true")){
            Intent i= new Intent(MainActivity.this,SecondActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(i);
            finish();
        }else{
             setContentView(R.layout.activity_main);
            }
}
Shibin Raju Mathew
  • 920
  • 4
  • 18
  • 41

2 Answers2

1

Problem solved by just disabling window preview in style like below

    <item name="android:windowDisablePreview">true</item>

Spend some hours to find the solution so here is the full description of problem

Why Window preview?

Android need some time for load data (on start) from class,resources etc so to overcome android displays a temporary window called Window preview. Which gives the user immediate feedback that the app launched and it also gives the app time to initialise itself. When your app is ready to run, the system removes the preview window and displays your app’s windows and views. As a result, none of your Java code is executed when the starting window is displayed.

Shibin Raju Mathew
  • 920
  • 4
  • 18
  • 41
  • where this windowdisablepreview implemented in aosp? – Shadow Jul 07 '23 at 10:20
  • Because I have a problem that, when white screen is displayed, immediately when user presses home button, it is not redirecting to launcher screen. – Shadow Jul 07 '23 at 10:21
0

setContentView shouldn't take effect (and any effort for Activity) as finish() is called earlier inside onCreate, but just to be shure add return to bracket

what you are seeing is windowBackground color, which may be changed in theme of Activity

// theme for activity, not whole application, will not affect other activities
<style name="MainActivityTheme" parent="AppTheme">
    <item name="android:windowBackground">#345678</item>

You may also change it by code just before super.onCreate call, but for a moment may be visible color from theme (especailly on low end devices) and you don't want this

maybe better approach is new Activity, which will be fully transparent and redirect straight to proper Activity? or if your session restoring is so heavy then use smth without visual representation like Service?

be careful with windowIsTranslucent flag, because your Activity will loose rotation configuration change calls

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • Actually showing one white intent and SecondActivity starts only after few second.(one intent coming foreground animation) – Shibin Raju Mathew Sep 03 '18 at 18:35
  • so set transparent `windowBackground` by XML for `MainActivity` as it resolves session too slow and until then it shouldn't be visible at all – snachmsm Sep 03 '18 at 18:38
  • But that badly affected other activity's background.. any other solution? – Shibin Raju Mathew Sep 03 '18 at 18:56
  • create separated style just for this one `MainActivity` (ans edited). set it in manifest by ``, keep `AppTheme` for `` tag and/or all other activities. and if you really need this background then set it just before `setContentView`, when `finish()` isn't called – snachmsm Sep 03 '18 at 19:33
  • 1
    problem solved by just disabling window preview,i know you spend lot time.. thank you. – Shibin Raju Mathew Sep 03 '18 at 19:38