Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Toast doesn't show up in the app/emulator.

Hey guys,

i have a problem, my toast doesnt show up in the app/emulator. I hope you can help me.

It also says "ActivityManager: Warning: Activity not started, its current task has been brought to the front" But why? I did exactly what was said in the video.

Thank you all!

It is the code from the 1st video in Integorrating the App > Toast Notification.

Here my code:

package com.example.crystal.ball;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.crystal.ball.ShakeDetector.OnShakeListener;

public class MainActivity extends Activity {

    private CrystalBall mCrystalBall = new CrystalBall();
    private TextView mAnswerLabel;
    private ImageView mCrystalBallImage;
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private ShakeDetector mShakeDetector;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Assign Views from Layout files
        mAnswerLabel = (TextView) findViewById(R.id.textView1);
        mCrystalBallImage = (ImageView) findViewById(R.id.imageView1);
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mShakeDetector = new ShakeDetector(new OnShakeListener() {

            public void onShake() {
                handleNewAnswer();

            }
        });


     Toast.makeText(this, "Yay! Our Activity was created!", Toast.LENGTH_LONG).show();   
    }

    @Override
    public void onResume() {
        super.onResume();
           mSensorManager.registerListener(mShakeDetector, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
    }

    @Override
    public void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(mShakeDetector);

    }
    private void animateCrystalBall() {

        mCrystalBallImage.setImageResource(R.drawable.ball_animation);
        AnimationDrawable ballAnimation = (AnimationDrawable) mCrystalBallImage.getDrawable();
        if (ballAnimation.isRunning()) {
            ballAnimation.stop();
        }
        ballAnimation.start();
    }

    private void animateAnswer() {
        AlphaAnimation fadeInAnimation = new AlphaAnimation(0, 1);
        fadeInAnimation.setDuration(1500);
        fadeInAnimation.setFillAfter(true);
        mAnswerLabel.setAnimation(fadeInAnimation);
    }

    private void playSound() {
        MediaPlayer player = MediaPlayer.create(this, R.raw.crystal_ball);
        player.start();
        player.setOnCompletionListener(new OnCompletionListener() {

            public void onCompletion(MediaPlayer mp) {
                mp.release();

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private void handleNewAnswer() {
        String answer = mCrystalBall.getAnAnswer();
        //Update the label with out dynamic answer
        mAnswerLabel.setText(answer);
        animateCrystalBall();
        animateAnswer();
        playSound();
    }
}

1 Answer

In Eclipse you will get the message "ActivityManager: Warning: Activity not started, its current task has been brought to the front" if you try to run your application if it is already installed, and your code hasn't been changed. If you simply delete a character and type it back in, it will re-install the application for you when you press the run button.

In addition, your toast is being activated in onCreate, which means it will only show the toast the very first time the activity is started/created. Here are some ways to have onCreate be called again.

  1. The app is re-installed (remember what I mentioned above. An application will only re-install if the code has been changed)
  2. The application is closed by the user (Note: 1. Hitting the home button doesn't close an application. 2. Hitting the back button WILL finish the activity. 3. Swiping the app away in android 4.0 and higher. 4. Going into the application manager and force closing the app)
  3. The application is terminated by the OS
  4. The device is rotated while viewing the app

With that being said, you should try to learn the Android Activity life-cycle, as it is very important to understand.

http://developer.android.com/reference/android/app/Activity.html

Thank you so much! That really helped me!

Cheers