Android的:如何获得allowTaskReparenting ="真"上班 [英] Android: How to get allowTaskReparenting="true" to work

查看:318
本文介绍了Android的:如何获得allowTaskReparenting ="真"上班的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写可以从其他应用程序通过接收与ACTION_VIEW或ACTION_EDIT意图启动的应用程序。例如,它可以通过查看电子邮件附件被打开。麻烦的是,当你点击home键和您所使用的电子邮件应用程序的启动图标再次点击,我的活动被杀害,并且已经取得的任何用户编辑会丢失。我希望发生的是,当用户点击home键,我的活动重新父,使其在用户点击我的应用程序的启动图标恢复。我试过设置的android:allowTaskReparenting = manifest.xml文件中的真实,但是这是行不通的。有时,它没有任何效果都没有,有时活动转移到我的启动图标,但仍当你在电子邮件应用程序的图标,再次点击就会被杀死。
在allowTaskReparenting的文档非常模糊。它说,物业的意思是:

I am writing an app that can be launched from another app by receiving an intent with ACTION_VIEW or ACTION_EDIT. For example, it can be opened by viewing an email attachment. The trouble is that when you click on the home button and click again on the launch icon of the email app you were using, my activity is killed and any user edits that had been made are lost. What I want to happen is that when the user clicks the home button, my activity is re-parented so that it resumes when the user clicks on the launch icon of my app. I've tried setting android:allowTaskReparenting="true" in manifest.xml but this doesn't work. Sometimes it doesn't have any effect at all, and sometimes the activity is moved to my launch icon, yet still gets killed when you click again on the email app icon. The documentation on allowTaskReparenting is really vague. It says the property means:

不管是不是活动的可以摆脱它开始它有亲和力的任务的任务。

"Whether or not the activity can move from the task that started it to the task it has an affinity for."

这个词是什么可以的意思吗?我要的是保证该活动的确实移动(并保持有)。有什么办法来实现这一目标?

What does the word can mean here? What I want is a guarantee that the activity does move (and stays there). Is there any way to achieve this?

在此先感谢任何人谁可以提供帮助。

Thanks in advance to anyone who can help.

修改

在应对下面的评论,我已经把一个婴儿版展示我遇到的问题。当你点击一个文件中的其他应用程序启动EditFileActivity(例如,一个附件的邮件),然后你可以编辑该文件。但点击主页图标,然后在电子邮件应用程序的图标,再次单击会导致丢失您对文件所做的更改。我想android系统的只有忘记EditFileActivity的一个实例,如果用户点击明确回来,然后说是或否。理想我想EditFileActivity的所有实例堆积,我的应用程序的启动图标。我可以实现通过使用singleTask或singleInstance,写一些显示选项卡所有打开的文件活动,与此类似,但它会容易得多,如果我能得到Android系统本身来帮助我。任何想法?

In response to comments below, I have put together a baby version demonstrating the problems I am encountering. When you start EditFileActivity by clicking on a file in another app (e.g, an attachment to an email) you can then edit the file. But clicking on the home icon and then clicking again on the email app icon causes the changes you have made to the file to be lost. I want the android system to only forget about an instance of EditFileActivity if the user explicitly clicks back and then says "yes" or "no". Ideally I want all instances of EditFileActivity to stack up on my app's launch icon. I could implement something similar to this by using singleTask or singleInstance and writing some kind of activity showing all open files in tabs, but it would be much easier if I could get the android system itself to help me. Any ideas?

下面是一个完整的项目展示的问题。

Here is a complete project demonstrating the problem.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
         package="com.example.Example"
         android:versionCode="1"
         android:versionName="1.0">
   <uses-sdk
       android:minSdkVersion="11"
       android:targetSdkVersion="11"/>
   <application
       android:label="Example"
       android:icon="@drawable/ic_launcher">
       <activity
           android:name=".LaunchActivity"
           android:label="LaunchActivity"
           android:screenOrientation="portrait">
           <intent-filter>
               <action android:name="android.intent.action.MAIN"/>
               <category android:name="android.intent.category.LAUNCHER"/>
           </intent-filter>
       </activity>
       <activity
           android:name=".EditFileActivity"
           android:label="EditFileActivity"
           android:screenOrientation="portrait">
           <!-- This is just an example. I wouldn't use this intent filter in a real app! -->
           <intent-filter>
               <action android:name="android.intent.action.VIEW"/>
               <action android:name="android.intent.action.EDIT"/>
               <category android:name="android.intent.category.DEFAULT"/>
               <category android:name="android.intent.category.BROWSABLE"/>
               <data android:scheme="file"/>
               <data android:scheme="content"/>
               <data android:mimeType="*/*"/>
               <data android:host="*"/>
           </intent-filter>
       </activity>
   </application>
</manifest>` 

LaunchActivity:

LaunchActivity:

public class LaunchActivity extends Activity {

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       TextView textView = new TextView(this);
       textView.setText("This is the activity you see when you click on the application's launch icon. It does absolutely nothing.");
       textView.setTextSize(18);
       setContentView(textView);
   }
}

EditFileActivity:

EditFileActivity:

public class EditFileActivity extends Activity {

   // This String represents the contents of the file.
   // In a "real" app the String would be initialised by reading the data from the Intent that started the activity.
   // However, for the purposes of this example, the initial value is "Default".
   private String fileContents = "Default";
   private boolean editsMade = false;
   private TextView textView;

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       textView = new TextView(this);
       textView.setText(fileContents);
       textView.setTextSize(18);
       textView.setPadding(10, 10, 10, 10);
       setContentView(textView);
       textView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               makeEdits();
           }
       });     
   }

   @Override
   public void onBackPressed() {
       if (editsMade) {
           savePrompt();
       } else {
           finish();
       }
   }

   private void savePrompt() {
       DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int which) {
               if (which == Dialog.BUTTON_POSITIVE) {
                   // Here is where I would save the edited file.
                   Toast.makeText(EditFileActivity.this, "File saved", Toast.LENGTH_LONG).show();
               }
               finish();
           }
       };
       new AlertDialog.Builder(this)
               .setTitle("Close File")
               .setMessage("Do you want to save the changes you made?")
               .setPositiveButton("Yes", listener)
               .setNegativeButton("No", listener)
               .show();
   }

   private void makeEdits() {
       final EditText editText = new EditText(this);
       editText.setText(fileContents);
       new AlertDialog.Builder(this)
               .setTitle("Edit File")
               .setView(editText)
               .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int whichButton) {
                       Editable editable = editText.getText();
                       assert editable != null;
                       String newContents = editable.toString();
                       if (!fileContents.equals(newContents)) {
                           editsMade = true;
                           fileContents = newContents;
                           textView.setText(fileContents);
                       }
                   }
               })
               .setNegativeButton("Cancel", null)
               .show();
   }
}

更新2014年10月12日

遇到的问题是由于使用了意图标志FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET的。
幸运的是,谷歌拥有德precated这个标志,作为API级别的21。

The problems encountered were due to the use of the Intent flag FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET. Fortunately, Google have deprecated this flag, as of API Level 21.

推荐答案

问题

麻烦的是,当你点击home键,然后再次单击
  您正在使用的电子邮件应用程序的启动图标,我的活动
  死亡,已经作出的任何用户编辑会丢失。

The trouble is that when you click on the home button and click again on the launch icon of the email app you were using, my activity is killed and any user edits that had been made are lost.

这是因为电子邮件应用程序中设置了 FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET 意图国旗同时发动你的活动。当这个标志设置,当任务带到前台接下来的时间,你的活动将结束,从而使用户返回到previous活动。

This happens because the email application had set FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET intent flag while launching your activity. When this flag is set, next time when the task is brought to the foreground, your activity will be finished, so that user returns to the previous activity.

从<一个href=\"http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET\"相对=nofollow>文档:

这是,你必须在一个合理的休息情况下非常有用的
  应用。例如,电子邮件应用程序可以具有到命令
  查看附件,这将启动图像视图活动显示
  它。这一活动应在电子邮件应用程序的任务的一部分,
  因为它是用户参与的任务的一部分。但是,如果
  用户离开该任务,后来从家里选择电子邮件应用程序,
  我们可能喜欢他们回到他们查看的谈话,不
  图片附件,因为这是令人困惑的。通过设置该标志
  启动图片浏览器,该浏览器和任何活动时,它
  启动会在用户下一次返回到邮件删除。

This is useful for cases where you have a logical break in your application. For example, an e-mail application may have a command to view an attachment, which launches an image view activity to display it. This activity should be part of the e-mail application's task, since it is a part of the task the user is involved in. However, if the user leaves that task, and later selects the e-mail app from home, we may like them to return to the conversation they were viewing, not the picture attachment, since that is confusing. By setting this flag when launching the image viewer, that viewer and any activities it starts will be removed the next time the user returns to mail.

解决方案
使用 singleTask launchMode为您的活动。电子邮件应用程序不会杀了你的活动,作为活动现在属于不同的任务。

Solution: Use singleTask launchMode for your activity. The email app will not kill your activity, as the activity belongs to different task now.

如果活动实例已经在任务和试图再次启动活动,那么一个新的实例不被创建。相反 onNewIntent 被调用。在这里,您可以提示用户保存previous编辑如果有的话,之前presenting新的内容。

If the activity instance is already in the task and an attempt is made to launch the activity again, then a new instance is not created. Instead onNewIntent is called. Here you can prompt the user to save the previous edit if any, before presenting new content.

这篇关于Android的:如何获得allowTaskReparenting =&QUOT;真&QUOT;上班的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆