Reputation: 83
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"OnCreate()");
setContentView(R.layout.activity_main);
mToolbar = findViewById(R.id.my_toolbar);
mTextview = findViewById(R.id.title);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragment, new OneFragment());
//ft.commit();
ft.addToBackStack("frag");
ft.commitAllowingStateLoss();
}
}, 5000);
}
after minimizing my android app again its opening automatically.please help me to solve this problem
Upvotes: 0
Views: 155
Reputation: 7148
The Correct way to manage the handler is, In your activity add this code,
private void initTasks() {
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
};
handler.postDelayed(runnable, 5000);
}
@Override
protected void onResume() {
super.onResume();
initTasks();
}
Then to stop opening your app after minimizing,
@Override
protected void onStop() {
super.onStop();
handler.removeCallbacks(null);
}
Upvotes: 1