开发者

Doubt with KeyEvent.KEYCODE_BACK in Android

I have 3 activities. 1st activity has a 'go right' image button. (to move to 2nd activity) 2nd activity has both 'go right' and 'go left' image buttons. (to go back to 1st activity or move to 3rd activity). 3rd activity has a 'go left' button to move to 2nd activity. And I have created events for back key press.

Problem is, You navigate from 1st to 3rd, come back and then navigate to 3rd activity. 1st activity---clicking go right----> 2nd activity---clicking go right-----> 3rd activity---clicking go left----> 2nd activity----clicking go left----> 1st activity----clicking go right----> 2nd activity---clicking go right-----> 3rd activity.

Now if I press back key on the 3rd activity, it moves to 2nd. and on pressing back key here, it moves to 1st. But here if I press back key, it again goes to 1st activity. Only the 2nd back key press on 1st activity exits the application. An additional question is how do I make the Imagebutton visible only If I press there?

Do I make any sense?

FirstActivity.java

package com.stylingandroid.Animation;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class FirstActivity extends Activity
{
    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.first );
        ((ImageButton)findViewById( R.id.NextButton )).setOnClickListener( new OnClickListener()
        {
            @Override
            public void onClick( View v )
            {
                startActivity( new Intent( FirstActivity.this, SecondActivity.class ) );
                overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left );
            }
        });
    }
    @Override
    public boolean onKeyUp( int keyCode, KeyEvent event )
    {
        if( keyCode == KeyEvent.KEYCODE_BACK )
        {
            finish();
            return true;
        }
        return super.onKeyUp( keyCode, event );
    }

    public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.options_menu, menu);
          return true;
        }

    public boolean onOptionsItemSelected(MenuItem item) 
    {
          switch (item.getItemId()) 
          {
          case R.id.next:
              startActivity( new Intent( FirstActivity.this, SecondActivity.class ) );
              overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left );
              return true;

          case R.id.exit:
                finish();

          default:
                return super.onOptionsItemSelected(item);
          }
    }

}
开发者_如何学编程

SecondActivity.java

package com.stylingandroid.Animation;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class SecondActivity extends Activity
{
    private ImageButton nextbutton;
    private ImageButton backbutton;
    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.second );
        nextbutton = (ImageButton) findViewById(R.id.NextButton);
        backbutton = (ImageButton) findViewById(R.id.BackButton);
        nextbutton.setOnClickListener( new OnClickListener()
        {
            @Override
            public void onClick( View v )
            {
                startActivity( new Intent( SecondActivity.this, ThirdActivity.class ) );
                overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left );
            }
        });
        backbutton.setOnClickListener( new OnClickListener()
        {
            @Override
            public void onClick( View v )
            {
                finish();
                startActivity( new Intent( SecondActivity.this, FirstActivity.class ) );
                overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right );
            }
        });
    }

    @Override
    public boolean onKeyUp( int keyCode, KeyEvent event )
    {
        if( keyCode == KeyEvent.KEYCODE_BACK )
        {
            finish();
            overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right );
            return true;
        }
        return super.onKeyUp( keyCode, event );
    }
}

ThirdActivity.java

package com.stylingandroid.Animation;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

public class ThirdActivity extends Activity
{
    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.third );
        ((ImageButton)findViewById( R.id.BackButton )).setOnClickListener( new OnClickListener()
        {
            @Override
            public void onClick( View v )
            {
                goBack();
            }
        });
    }

    private void goBack()
    {
        finish();
        overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right );
    }

    @Override
    public boolean onKeyUp( int keyCode, KeyEvent event )
    {
        if( keyCode == KeyEvent.KEYCODE_BACK )
        {
            goBack();
            return true;
        }
        return super.onKeyUp( keyCode, event );
    }
}


There's a bug here:

    backbutton.setOnClickListener( new OnClickListener()
    {
        @Override
        public void onClick( View v )
        {
            finish();
            startActivity( new Intent( SecondActivity.this, FirstActivity.class ) );
            overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right );
        }
    });

You are finish()ing and startActivity()ing FirstActivity as a result of which it is sitting on the Activity stack twice.

On a side note, what are you trying to achieve here?

@Override
public boolean onKeyUp( int keyCode, KeyEvent event )
{
    if( keyCode == KeyEvent.KEYCODE_BACK )
    {
        goBack();
        return true;
    }
    return super.onKeyUp( keyCode, event );
}

The system attaches the back button to finish(). You don't need to handle it at the onKey level.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜