Swapping between intents

edited February 2014 in Android Mode

I am learning how to use processing in the IDE (Android Studio / Eclipse) environment and I am having some trouble solidifying how I would go about properly moving between processing programs.

My app has one .java file that executes upon launch of the Android app. Lets call this the menu.java. When we click a button on this application that says "Play" I am calling:

super.onDestroy();
Intent intent = new Intent(this, GameRun.class);
startActivity(intent);

In the file GameRun.java is where the processing program #2 runs which will be a game. This works fine, but things start to get bumpy when the game closes and returns back the the menu using:

super.onDestroy();
Intent intent = new Intent(this, Menu.class);
startActivity(intent);

If I tried to get back to GameRun a second time, it does not execute. I think I am not properly opening or closing these programs.

//////////////////////////

The second problem I am having is with APMediaPlayer from the import apwidgets.* library.

In the Menu application, I am using @Override on onStart(), onDestroy(), onPause() and onResume() to handle closing the app to the menu (pause) or returning from GameRun.java to Menu.java to restart the meda player.

Sometimes, the player doesn't play the audio file and in the DDMS I see an error:

E/MediaPlayer﹕ Should have subtitle controller already set

Which only happens when the sound doesn't play. If I tap the volume button or something the audio starts to play again.

Here is the logic for the player:

    @Override
    public void onPause() {
        super.onPause();
        if(player != null) {
            player.pause();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(player != null) {
            player.release();
            player = null;
        }
    }


    @Override
    public void onStart() {
        super.onStart();
        if(player != null) {
            player.start();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if(player != null) {
            player.start();
        }

    }

I am new to Android development as well as new to this form, so please be easy on me!

Answers

  • This isn't the way that activities were designed to be used. You shouldn't kill your activity before moving onto another one that is deeper in the hierarchy - instead, leave it there, and simply start the new activity on top of it:

    //Play game
    Intent intent = new Intent(this, GameRun.class);
    startActivity(intent);
    

    Now, when you want to navigate back up the hierarchy, since you haven't killed the old activity, you need only to get rid of the current activity (also, the correct way to do this is finish(), which may fix some of your problems right away...). This way, the previous state of the parent activity is retained:

    //Return to menu
    finish();
    

    This type of activity life cycle should solve most of your problems. Please keep in mind that Processing wasn't necessarily designed to be used like this... which is why many Processing apps manage their own internal life cycle. You may very well get this to work, however.

    I don't know what it causing the problem with APWidgets at the moment... perhaps I can look into it a bit later... For one thing, though, I would make sure to clean up anything before calling the super method (in onPause() and onDestroy()), because calling the parent method essentially kills your activity:

    @ Override
    public void onDestroy() {
        //Do this first
        if(player != null) {
            player.release();
            player = null;
        }
        super.onDestroy();
    }
    
  • Since you say that it is bad practice to kill the activity before moving to another, then I am assuming you are implying that I should be toggling between the two.

    Is there any way to switch between two intents? If so, when would the parent intent be initialized? What would it be called?

  • I'm afraid I don't entirely understand your question, and this may be because you don't understand how intents / the activity life cycle work. I'll attempt to summarize:

    In the Android ecosystem, an Activity is a "screen" of an app. It is content of the application that is directly visible to the user. A Processing sketch has only one activity, but a "normal" Android app has several: a start activity, a details activity, a settings activity, etc. The user moves from one activity to the next.

    An app can normally be represented as a hierarchy. For example, the starting screen is at the top. Further down, there may be a details screen. There may also be a settings screen, and any number of other screens that branch off from these screens. When the user presses the back button, they return to the previous screen. When the user presses the up button (in the top left corner), they navigate "up" the activity hierarchy. I really wish I had a nice graphic, but...

    You use intents to navigate between activities - but only when starting new activities (so "further in" to the hierarchy - down and out, but not up). An intent is a messenger that passes information to the Android system. In this case, you give it a very specific message: "Start activity GameRun." When you call on this intent, the activity GameRun is created "further down" the activity hierarchy - so the activity that you came from is still there, but it's covered up by the new activity. When you're done with GameRun and want to return Menu, you don't need an intent because the activity is still there in the activity hierarchy. You simply close the current activity and move "up" the hierarchy (with finish()). Now that you have closed the activity, the GameRun activity is terminated and no longer exists - you must create a new intent to return to it. You return to the Menu activity that is in the same place that you left it last because it was not terminated.

    Hopefully my explanation makes some sort of sense. In the off chance that I'm merely spewing gibberish (a decent probability), here are a couple of links:

    http://developer.android.com/guide/components/activities.html http://developer.android.com/training/basics/activity-lifecycle/starting.html http://developer.android.com/guide/components/intents-filters.html http://www.vogella.com/tutorials/AndroidIntent/article.html

Sign In or Register to comment.