Search code examples
javaandroideclipseonclickpostdelayed

Canceling postDelayed with button (onClick)


help me for the coding. I am not good at coding java and xml, so help.

Intro activity shows for 1500ms, and after that, main activity shows. I want to add skip button in intro activity.

When the skip button is clicked, I want to skip to main activity.

This is my code:

MainActivity.java

package com.bedrock.schedule;

import android.R.menu;
import android.R.anim;
import android.R.layout;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;


public class MainActivity extends Activity {

private BackPressCloseHandler backPressCloseHandler;

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

    //Start Transition Animation
    this.overridePendingTransition(R.anim.startenter, R.anim.startexit);
    setContentView(R.layout.activity_main);

    //Image Animation
    ImageView image = (ImageView)findViewById(R.id.Logo);
    Animation animation=AnimationUtils.loadAnimation(this, R.anim.logoenter);
    image.startAnimation(animation);

    //Image Homepage Link
    image.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_BROWSABLE);
            intent.setData(Uri.parse("http://young-dong.ms.kr"));
            startActivity(intent);
        }
    });

    //Exit Toast
    backPressCloseHandler = new BackPressCloseHandler(this);
}

//Button Activity Link
public void ClassMain(View view) 
{
    Intent intent = new Intent(MainActivity.this, ClassMain.class);
    startActivity(intent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

//Exit Toast
@Override
public void onBackPressed() {
    //super.onBackPressed();
    backPressCloseHandler.onBackPressed();
}

//Finish Transition Animation
@Override
public void finish() {
    super.finish();
    this.overridePendingTransition(R.anim.endenter, R.anim.endexit);
}

}

IntroActivity.java

package com.bedrock.schedule;

import com.bedrock.schedule.R;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class IntroActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.overridePendingTransition(R.anim.startenter, R.anim.startexit);
    setContentView(R.layout.activity_intro);

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {
            Intent intent = new Intent(IntroActivity.this, MainActivity.class);
            startActivity(intent);

            //Don't show on back - Finish
            finish();
        }
    }, 1500);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.intro, menu);
    return true;
}

public void Skip(View view) 
{
    Intent intent = new Intent(IntroActivity.this, MainActivity.class);
    startActivity(intent);
}

}

Help... If you can write down the code, please. I'm stuck in this and can't pubish to app store...


Solution

  • Very simple, you don't even need to cancel the Runnable. Just add:

    private boolean mAlreadyGone;
    

    in the class. Then, in both the Skip() method and the Runnable code, start with:

    if (mAlreadyGone)
        return;
    
    mAlreadyGone = true;