簡體   English   中英

Android的活動堆棧-從URI啟動的PDF返回無法返回正確的活動

[英]Activities stack android - back from a URI launched pdf doesnt return to correct activity

我全神貫注地試圖弄清為什么我的活動之一無法通過stack.back按鈕正常工作。

當用戶在“主要活動”中按下圖像按鈕時,將啟動“活動大腦”,這可以正常工作,並且后退按鈕的行為與“大腦”預期的一樣(即,它按預期返回主要活動)。

我想在大腦中根據用戶按下的圖像按鈕啟動PDF,我通過以下代碼通過URI進行操作,PDF可以很好地加載,但是在acrobat中按下后,應用程序將返回Main活動,而不是大腦這不是我想要的(或預期的)行為。 我已經閱讀了應用程序開發人員指南,並嘗試了使用Intent Flag_activity的所有選項(我認為),但無法修復后退按鈕的行為。 我曾嘗試鏈接到不同的PDF(以防雜技演員中的東西是隨機的),但該應用程序的行為相同,總是回到主目錄,而不是我想要的大腦。

我正在使用Imagebuttons來允許用戶選擇pdf,並且下面的代碼正在調用URI /意圖來顯示pdf。 我已經離開了2個嘗試過的Intent.Flags,但沒有運氣可以放大,我期望Intent.Flag_Activity_Clear_Top可以工作,但是我一定錯過了一些東西。

使用以下onClick代碼從主要位置調用大腦活動

 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);
                    }

然后大腦活動java看起來像這樣:

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);
            }

}

我不確定在編寫此應用程序時是否錯過了其他地方? 下面是清單上的大腦和主要活動部分,如果有什么不同的話

   <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>

我完全不知道為什么該代碼無法正常工作,我在其他活動中從其他地方啟動PDF,而URI的方式運行良好,我錯過了一些東西,但希望能有新的眼光發現我不能做到的事情!

希望我提供了足夠的細節,如果沒有,請告訴我應該包括的內容。 謝謝你的幫助,安迪

因此,在此處關注此帖子后,請查看任務的活動堆棧

我的活動堆棧從主要活動到大腦活動打印如下(正確)

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}

到目前為止,我認為一切都很好,但是當我從大腦活動啟動任何pdf時,這就是結果活動堆棧:

      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}

因此,出於某種原因,打開PDF會啟動主活動的另一個版本,但是我根本不知道為什么會發生這種情況? 希望這對其他人意味着什么?

感謝您可以為此提供的任何幫助。

因此,獲取此代碼,將Brain的所有代碼復制並粘貼到名為Brain 2的新活動中,該應用程序將正常運行。 不知道為什么,不是一個整潔的解決方案,但至少它能起作用! 我曾嘗試清理該項目,但沒什么區別。 至少現在已經排序了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM