OnkeyDown / onBackPressed not working with intent?
This is a simple operation and seems to work until I change my minSdk = 8 to 9 and targetSdk remains 21 in my manifest. The problem is that I have activity A and I go to activity B when the button in activity A is clicked. Now about activity B, whenever someone presses the button, I want to clear the activity stack and pass my activity B to activity C. but instead it exits activity B and goes to Activity A, so far I have tried onKeyDown, onBackPressed , nothing works, kindly help.
Action A ( in onClick method ):
Intent in = new Intent(A.this,
B.class);
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(in);
[ UPDATED ] Activity B:
@Override
public void onBackPressed() {
Intent in = new Intent(this, C.class);
Bundle bundle = new Bundle();
bundle.putSerializable("Scores", score);
in.putExtras(bundle);
startActivity(in);
}
Action C:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.C);
ScoreSaver scores = (ScoreSaver) getIntent().getExtras().getSerializable("Scores");
}
Add android:noHistory="true"
to activities A and B in your manifest file. This way, these activities won't be in your back stack. In activity B, you must override the behavior onBackPressed()
. It should look like this:
@Override
public void onBackPressed() {
Intent intent = new Intent(this, ActivityC.class);
startActivity(intent);
}
If you leave super.onBackPressed()
in your method, the default behavior will happen, i.e. activity B will be closed and you will return to activity A.
source to share
Unfortunately, I don't have a Git account, so I post the contents of the files here. (Application theme inherits from Theme.AppCompat.Light.DarkActionBar
)
manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fox.chris.activitytest">
<application android:allowBackup="true"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:theme="@style/AppTheme">
<activity
android:name=".ActivityA"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ActivityB"
android:noHistory="true">
</activity>
<activity android:name=".ActivityC">
</activity>
</application>
</manifest>
Action A:
public class ActivityA extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
}
});
}
}
Activity B:
public class ActivityB extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
}
@Override
public void onBackPressed() {
Intent intent = new Intent(this, ActivityC.class);
startActivity(intent);
}
}
Action C:
public class ActivityC extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c);
}
}
source to share