我有两个页面1.MainActivity 2.Next page.I我每10秒后刷新下一页。问题是一旦我点击后退到主活动页面,它会在下一页再次刷新。不希望它直接进入下一页。我已尝试使用onBackPressed按钮,但它不起作用。 以下是我的代码: -
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>
答案 0 :(得分:3)
@Override
public void onBackPressed() {
handler.removeCallbacksAndMessages(null);
finish();
}
只有onBackPressed中的finish(),删除super.onBackPressed();
尝试它&amp;让我知道