1

I have two pages 1.MainActivity 2.Next page.I am refreshing Next page after every 10 seconds.The problem is once I click back and come to Main Activity page it goes again in Next page as it get refreshed.I dont want it to go directly to Next page.I have tried with onBackPressed button but it does not work. Following is my code:-

MainActivity.java

public class MainActivity extends Activity {
    Button next;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        next=(Button)findViewById(R.id.refresh);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,Next.class);
                startActivity(intent);

            }
        });
    }
}

NextPage.java

public class Next extends Activity {
    ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.next);
        iv=(ImageView)findViewById(R.id.imagev);

         Toast.makeText(Next.this, "Refreshed", Toast.LENGTH_SHORT).show();



        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                Intent intent = getIntent();
                overridePendingTransition(0, 0);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                finish();
                overridePendingTransition(0, 0);
                startActivity(intent);
            }
        }, 10000);




    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.ram.refresh.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/hello"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/refresh"
        android:text="Next page"
        android:layout_below="@+id/hello"/>
</RelativeLayout>

next.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imagev"
        android:src="@drawable/smiley"/>

</LinearLayout>
riya
  • 17
  • 5
  • Simple and standard solution create a stack of activity using array list. [Here is the solution](https://stackoverflow.com/questions/38326642/how-to-remove-a-particular-activity-from-android-back-stack/38326848#38326848) – Andy Developer Jun 20 '17 at 09:58

1 Answers1

3
 @Override
        public void onBackPressed() {
handler.removeCallbacksAndMessages(null);
            finish();
        }

Have only finish() in onBackPressed, remove super.onBackPressed(); try it & let me know

Moulesh
  • 2,762
  • 1
  • 22
  • 32