-2

Hi I have strange behavior of my activities. From MainActivity i start another activity and everything is cool. If I press back key the app doesn't go back to previous activity but it return me to desktop.

I have not called destory or finish in MainActivity when I have started the other activity.

Few hours before I change some services in the app and everything worked fine and suddenly it started to behave in this way. I have not change anything regarding activity calling or so.

public class EpgActivity extends Activity {


   private EpgRectangleView epgRectangleView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.onCreate(savedInstanceState);


    epgRectangleView = new EpgRectangleView(this);
    epgRectangleView.setBackgroundColor(Color.BLUE);

    View decorView = getWindow().getDecorView();
    int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

    decorView.setSystemUiVisibility(uiOptions);

    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    setContentView(epgRectangleView);

    System.out.println("BOOSTED: " + epgRectangleView.isHardwareAccelerated());

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    epgRectangleView.getEpgScreenService().onClick(keyCode);

    epgRectangleView.invalidate();

    epgRectangleView.clearAnimation();

    return super.onKeyDown(keyCode, event);
}

}

Any idea what could cause this behavior?

public class MainActivity extends Activity {

private MainViewModel mainViewModel;
private ActivityMainBinding binding;

private DrawerLayout drawerLayout;


@Override
public void sendBroadcast(Intent intent) {
    super.sendBroadcast(intent);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View decorView = getWindow().getDecorView();

    /*int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
    */


    int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

    decorView.setSystemUiVisibility(uiOptions);

    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    setContentView(R.layout.activity_main);

    initDataBinding();

}

private void initDataBinding() {

    binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

    mainViewModel = MainViewModel.getInstance(this);

    binding.setMainViewModel(mainViewModel);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    drawerLayout.openDrawer(GravityCompat.START);

    Handler handler = new Handler();

    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            //Do something after 100ms
            drawerLayout.closeDrawer(Gravity.LEFT);
        }
    }, 1200);

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

//        Toast.makeText(this,event.getKeyCode() + "", 

Toast.LENGTH_SHORT).show();
        if (keyCode == KeyEvent.KEYCODE_ENTER) {
            if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                drawerLayout.closeDrawer(Gravity.LEFT);
            } else {
                drawerLayout.openDrawer(GravityCompat.START);
            }
        }
    if (keyCode == KeyEvent.KEYCODE_X) {

        startActivity(new Intent(this, EpgActivity.class));

    }

    return super.onKeyDown(keyCode, event);
}
}

Please keep in mind that object ViewModel doen't have anything that could harm activity lifecycle.

svarog
  • 688
  • 1
  • 12
  • 29

3 Answers3

2

These are the ways you are not able to go previous Activity.

  1. If you written Flags it will clear the stack

         Intent homeActivity = new Intent(context, DJ_HomeActivity.class);     
         homeActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK,Intent.FLAG_ACTIVITY_CLEAR_TOP); // it will clear the stack so remove the line if you written
         context.startActivity(homeActivity);
    
  2. In Manifest file

         android:noHistory="true" // If you written in Manifest remove this line
    
  3. While moving activity from one class to another class

      Intent homeActivity = new Intent(context, DJ_HomeActivity.class);     
      context.startActivity(homeActivity);
      finish(); // // If you written in Activity remove this line
    
  4. Remove lauchMode or put standard in Manifest file

     android:launchMode="standard"
    
Rajasekhar
  • 2,345
  • 1
  • 13
  • 20
  • 1
    Had you read the question? *If I press back key the app **doesn't go back** to previous activity but it return me to desktop.* so obviously he don't wana close the app – Selvin Mar 22 '17 at 10:03
0

I guess you have set FLAG_ACTIVITY_CLEAR_TOP in your second Activity in Manifest.xml. Check it or add Manifest.xml and your related code snippet.

Awais
  • 1,157
  • 9
  • 15
0

When you're pressing back button, method onBackPressed executes. If you didn't override it, default will be processed. Default makes top activity in stack be focused. But if you don't have activities in stack, you will just exit. By executing finish(), you will kill your activity, and it won't be saved in stack. If you will just call new intent, without killing previous, onBackPressed will do exactly that you want.

To be familiar with activities lifecycle follow this link.

Nazacheres
  • 99
  • 10