Transition gives flicker when splash screen calls "finish()" after startActivity(intent, options.toBundle());

3.2k Views Asked by At

I am creating an android app (lollipop version). App shows large logo in middle of splash screen. Login screen contains a small sized logo at top. I use ActivityOptions.makeSceneTransitionAnimation() to set animation from large logo of splash screen to small logo of login screen.

Splash screen launches to launch app. After delay of few milisec , splash screen creates intent for login screen. Also set transition. Then it starts login activity. It begins transition animation of logo. And shows login screen successfully. Everything is working well and animation is smooth up to this point.

Then I added "finish();" in splash screen so that back button on login screen do not loads splash screen. Now transition was giving flicker.

I tried following approach but still flicker is there.

  1. used "finishAfterTransition();" instead of "finish();"
  2. added FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_NEW_TASK flags to intent (this leads to even wired behavior)
  3. Override onBackPressed() in login screen. and added
    finish(); android.os.Process.killProcess(android.os.Process.myPid()); - This terminates app but restarts it again.

Here, I'm pasting the code of splashScreen. This code is working but gives flicker while transition. gotoLoginScreen() method at the end of class is the place which loads login activity. The login screen is basic activity template from android studio. Using com.android.support:appcompat-v7:21.0.2 library to support lower devices.

public class SplashActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_splash, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    ImageView imageView_logo;

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_splash, container, false);
        imageView_logo = (ImageView) rootView.findViewById(R.id.imageview_logo);
        imageView_logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startTimer();
            }
        });
        startTimer();
        return rootView;
    }

    private void startTimer() {
        new CountDownTimer(1000, 1000) {
            @Override
            public void onTick(long l) {
            }

            public void onFinish() {
                launchNextActivity();
            }
        }.start();
    }

    /**
     * base on session continuity the next activity will be decided and
     * launches next activity
     */
    private void launchNextActivity() {
        if (isSessionContinue()) {
            goToHomeScreen();
        } else {
            goToLoginScreen();
        }
    }

    /**
     * checks current user ID, null indicate terminated session
     *
     * @return true if session is continued and false of session is terminated
     */
    private boolean isSessionContinue() {
        return false;
    }

    /**
     * directly leads to home screen
     */
    private void goToHomeScreen() {
    //code to start home screen by skipping login
    }

    /**
     * leads to login screen.
     */
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    **private void goToLoginScreen() {
        Intent loginIntent = new Intent(getActivity(), LoginActivity.class);
        if (android.os.Build.VERSION.SDK_INT >= 21) {
            ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(getActivity(), imageView_logo, getString(R.string.transition_logo));
            getActivity().startActivity(loginIntent, options.toBundle());
            getActivity().finishAfterTransition();
        } else {
            startActivity(loginIntent);
            getActivity().finish();
        }
    }**

}

}

Is there any way to avoid flicker? It is a stain on beauty.

1

There are 1 best solutions below

0
On

add this to your activity:

    private boolean shouldFinish = false;

    @Override
    public void onStop() {
      super.onStop();
      if (shouldFinish) {
        getActivity().finish();
      }
    } 

and change this:

    private void goToLoginScreen() { 

      shouldFinish = true;

      Intent loginIntent = new Intent(getActivity(), LoginActivity.class);
      if (android.os.Build.VERSION.SDK_INT >= 21) {
        ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(getActivity(), imageView_logo,    getString(R.string.transition_logo));
        getActivity().startActivity(loginIntent, options.toBundle()); 
      } else { 
        startActivity(loginIntent); 
      } 
    }