2

I'm new to Android programming, so I'm probably missing something trivial here.

The goal is to create a prototype of a digital signage app. Right now I've created three activities; MainActivity has a method which switches to the second activity after a certain period of time. The same method is then called from the second activity to third, and from third back to main.

There are two problems though: first, is it ok to create new Intent every time the app switches between the activities? As I mentioned, I started learning Android recently, so please don't be mad if this is a super dumb question.

Second: the app launches itself after I've hit home button in my emulator even though I'm calling finish(); after the startActivity(intent);

Here is the method in the MainActivity:

public void switchActivities(final Class<?> classObject) {
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            Intent intent = new Intent(getApplicationContext(), classObject);
            startActivity(intent);
            finish();
        }

    }, 1000);
}

The activities Second and Third extend MainActivity and call the switchActivities method: switchActivities(Third.class); (from Second to Third).

Thanks in advance!

UPDATED: I added public boolean isRunning = true; to my MainActivity and if(isRunning) startActivity(intent); to the switchActivities method; I also added a method

@Override
protected void onPause(){
    super.onPause();
    isRunning = false;
}

as advised here.

Although finish(); should have cleared the method stack, the app didn't close after pressing Home button but went to previous activity instead, so I added this line to every activity in the AndroidManifest file:

android:noHistory="true"

as advised here.

Sorry for not upvoting helpful answers, I don't have enough reputation yet.

5
  • Do you want to close when the home button pressed ?
    – Amsheer
    Commented Feb 7, 2014 at 12:44
  • @Amsheer, yes, that is the idea.
    – madness
    Commented Feb 7, 2014 at 12:48
  • Then override onpause() method in each activity and call finish() from onpause(). It will help.
    – Amsheer
    Commented Feb 7, 2014 at 13:00
  • Read activity life cycle. I am not sure you can catch home button press but activity moves like this protected void onStart(); protected void onRestart(); protected void onResume(); protected void onPause(); protected void onStop(); protected void onDestroy(); :developer.android.com/reference/android/app/Activity.html
    – Amsheer
    Commented Feb 7, 2014 at 13:03
  • i think you should detect when app(activity) moves to Background and remove messages from your handler.
    – con_9
    Commented Feb 7, 2014 at 13:26

3 Answers 3

1

Yeah, it is the proper way of switching activities by creating new intents. I am little confused about your second questions. if you are saying that as you are using postDelayed method which gets triggered after some interval, is being called even you have finished your activity or pressed home button, you can handle this by creating a boolean variable isRunning in your activity which gets false when onPause or onDestroy gets called. Then you in your postdelayed method, you can check the flag and then proceed as required.

1
  • Done! Sorry, I'm new here, didn't know!
    – madness
    Commented Feb 10, 2014 at 7:13
1

The Intent used in navigation between the Activities , so it's cool to using it , no problem

The second problem because the Activity object still in your memory after you hit Home button , then the Handler will continue its work and start the activity after the certain period you determined in your code

0

For your first question: it's not a problem that you're creating a new Intent every time

The second one is a bit trickier. When you press the home button the current activity stays in memory, so it can start the new one even though you think it's not running. The easiest way to handle this to check weather the activity is finishing, and if so, you don't show the next one.

if (!isFinishing()) startActivity(intent);

This if statement prevents your activity to start a new one when it's in the background.

Not the answer you're looking for? Browse other questions tagged or ask your own question.