简体   繁体   English

从URI向后按下时返回错误的活动,启动PDF

[英]Returning to wrong activity when back pressed from URI launched PDF

Im hoping this is simple, but im fairly confused as to what is going on. 我希望这很简单,但是我对所发生的事情相当困惑。 I am launching a PDF from an Imagebutton in an android activity using a piece of code I copied from elsewhere in my app. 我正在使用我从应用程序其他地方复制的一段代码从android活动中的Imagebutton启动PDF。

if (v==imagebutton20) {

                File file = new File("/sdcard/documents/20.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

The pdf loads fine but when i press back the activity that launched the pdf isnt show, instead it returns to the activity before. pdf加载正常,但是当我按回启动pdf的活动时,它并没有显示,而是返回到之前的活动。 I have tried commenting out the 我试图注释掉

     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

but the same effect. 但是效果一样。 What am i missing? 我想念什么? Sorry but i couldnt make sense of the official android guide, must be having a thick day! 抱歉,但我无法理解官方的android指南,一定度过了愉快的一天!


Even more confused as I have checked the manifest file, the .xml for the activity (Brain) and yet each time i load the pdf using the code above back takes me back to the wrong activity (main) in my app. 当我检查了清单文件,活动的.xml(大脑)时更加困惑,但是每次我使用上面的代码加载pdf时,都会使我回到应用程序中错误的活动(主目录)。 I am calling the Brain activity from Main via an imageview, but that works fine. 我正在通过图像视图从Main调用Brain活动,但是效果很好。 When i launch the PDF above from Brain the back button takes me to Main and not the launching activity Brain. 当我从Brain启动上面的PDF时,返回按钮会将我带到Main而不是Brain活动。

I am using the code above elsewhere in 2 other activites in my app and the back key behaves exactly as you want / expect. 我在我的应用程序中的其他2个活动中使用上面的代码,并且back键的行为完全符合您的期望/期望。 Thats why i started checking the mainfest file, but no idea what im missing. 那就是为什么我开始检查mainfest文件,但是不知道我错过了什么的原因。

ANY help would be ace please! 请提供任何帮助!

Thanks. 谢谢。

I cannot explain why the following worked, but in case anyone else has a similar issue I solved the problem by: 我无法解释以下原因的工作原理,但是如果其他人遇到类似的问题,我可以通过以下方法解决该问题:

  • Creating a new Android activity in eclipse. 在eclipse中创建一个新的Android活动。
  • Copying the code from the misbehaving activity. 从行为异常的活动中复制代码。
  • Pasting the code in the newly created activity. 将代码粘贴到新创建的活动中。

Simple and not really sure why it worked, but it did. 很简单,但不确定为什么会起作用,但是确实可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM