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>