I'm writting a text editor, and I use
<activity android:name=".Editor">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="file"/>
<data android:scheme="content"/>
<data android:scheme="http"/>
<data android:scheme="https"/>
<data android:mimeType="*/*"/>
</intent-filter>
</activity>
in AndroidManifest.xml
to let my editor can open file from other applications(for example, the default file broswer). But each time I open a file from other applications, my editor will create a new Editor
activity, which causes that my editor will have many views and I need to quit many times.
So I'm thinking that when creating a new Editor
activity before setContentView
, to check whether there exists another Editor
activity. If exists, switch to another Editor
activity and destroy the current activity:
public class Editor extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if () {// TODO if exists another Editor
;// TODO destroy current Editor
;// TODO switch to another Editor
} else {
setContentView(...);
...
}
}
}
If it is feasible, how to accomplish the method?
(Because I'm not familiar with java
/xml
, I don't know whether this problem can be solved by just editting the code of AndroidManifest.xml
.)
Add android:launchMode="singleTask"
into your Activity as below. This line makes sure that your activity has only one instance (the one created the latest). Once added this code, you can delete if
/else
check in your Activity.
<activity android:name=".Editor"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="file"/>
<data android:scheme="content"/>
<data android:scheme="http"/>
<data android:scheme="https"/>
<data android:mimeType="*/*"/>
</intent-filter>
</activity>