Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6217905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:30:25+00:00 2026-05-24T07:30:25+00:00

I have 3 activities. 1st activity has a ‘go right’ image button. (to move

  • 0

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 );
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:30:27+00:00Added an answer on May 24, 2026 at 7:30 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application I have several activities, the main screen has 4 buttons that
I have two activities. Home activity contain a list view with two buttons named
I have 2 activities. In activity A when pressing a button sends you to
I have an activity group containing 3 activities. When a button is pressed, I
I have 3 Activities...the first Activity have a button that start third Activity. The
i have 4 activities in which i have to swipe them right, left and
I have 2 activities. Activity A and Activity B. Both calling each other though
I have two Sharepoint lists: - Assignments - Activities The activities list has a
I have three activities 1 is the main activity 2 is some other activity
suppose that you have 3 Activities (A, B and C) and Activity A starts

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.