[Android Q&A, Help & Troubleshooting] geekyhawkes:
I am going out of my mind trying to work out why one of my activities is not working correctly with the stack.back button.

Activity Brain is launched when the user presses an image button in Main activity, this works fine and the back button behaves as expected from Brain (ie it returns to main activity as expected).

Within brain i want to launch a PDF based on which image button the user presses, I am doing this via URI with the code below, the PDF loads fine, but when back is pressed within acrobat the app returns to Main activity and not the brain which is not the behaviour i want (or expected). I have read the app developers guide and tried all options with Intent Flag_activity (i think) but have not been able to fix the back button behaviour. I have tried linking to different PDFs (in case it was something random in acrobat) but the app behaves the same, always going back to main and not the brain that I want.

I am using Imagebuttons to allow the user to select the pdfs and the code below is calling the URI / intent to display the pdf. I have left in 2 of the Intent.Flags I have tried and had no luck with for amplification, I had expected Intent.Flag_Activity_Clear_Top to work, but i must have missed something.

Brain activity is called from main using the following onClick code

if (v==bbrain) {
Intent startbrain = new Intent(Main.this, Brain.class); //this is the Brain chooser
startActivity(startbrain);
overridePendingTransition(R.anim.pull_from_left_enter, R.anim.pull_out_to_left_exit);
}

Then brain activity java looks like this:

public void onClick(View v) {

if (v==bhome); {
Intent starthome = new Intent(Brain.this, Main.class); //this is main screen
startActivity(starthome);
overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
}

if (v==babouttest){
Intent start1gp = new Intent(Brain.this, AboutScreen.class); //this is 1gp lesson
startActivity(start1gp);
overridePendingTransition(R.anim.pull_from_left_enter, R.anim.pull_out_to_left_exit);
}

if (v==imagepetro) {
File file = new File("/sdcard/documents/1.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

try {
startActivity(intent);
overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
}
catch (ActivityNotFoundException e) {

}
}
}	//end of load

if (v==imagesteam) {
File file = new File("/sdcard/documents/2.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

try {
startActivity(intent);
overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
}
catch (ActivityNotFoundException e) {

}
}
}	//end of load

}

//menu inflator bits
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_brain, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}

}

I am not sure if i have missed something elsewhere when writing this app? Below is the brain and main activity sections out of the manifest if that makes any difference

<activity
android:name=".Main"
android:label="@string/title_activity_main"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<!-- <category android:name="android.intent.category.LAUNCHER" /> -->
</intent-filter>
</activity>
<activity
android:name=".Brain"
android:label="@string/title_activity_brain"
android:screenOrientation="landscape" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.trainer.Main" />
</activity>

I am totally out of ideas why this code isnt working, i am launching PDFs from elsewhere in other activities and the URI way has worked fine, I am missing something but hopefully a fresh pair of eyes will spot what I cant!

So following this post here http://stackoverflow.com/questions/2442713/view-the-tasks-activity-stack

my activity stack prints out as follows from main to brain activity (correctly)

Running activities (most recent first):
Run #3: ActivityRecord{41cd3948 u0 com.trainer/.Brain}
Run #2: ActivityRecord{41ca29a0 u0 com.trainer/.Main}
Run #1: ActivityRecord{41a2c5d8 u0 com.cyanogenmod.trebuchet/.Launcher}

so all good so far I think, but when I launch any of the pdfs from the brain activity this is the resultant activity stack:

Run #5: ActivityRecord{4154ca00 u0 com.adobe.reader/.ARViewer}
Run #4: ActivityRecord{41bcbcf8 u0 com.trainer/.Main}
Run #3: ActivityRecord{41cd3948 u0 com.trainer/.Brain}
Run #2: ActivityRecord{41ca29a0 u0 com.trainer/.Main}
Run #1: ActivityRecord{41a2c5d8 u0 com.cyanogenmod.trebuchet/.Launcher}

So for some reason opening the PDF starts another version of the main activity, btu I have no idea at all why this is happening? Hopefully this means something to someone else?

Thanks for any help that can be offered on this....

Hopefully I have provided enough detail, if not please let me know what I should include. Thanks for any help, Andy