GAMA
GAMA

Reputation: 5996

Activity getting destroyed when starting another activity for file viewing

I'm performing file viewing operation as follows:

private void openFile(File file, String format) {
    try {
        if (file.exists()) {
            Uri path = Uri.fromFile(file);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            String mimeType = "*/*";
            if (format.length() > 0) {
                mimeType = MimeTypeMap.getSingleton()
                        .getMimeTypeFromExtension(format);
                if (mimeType == null)
                    mimeType = "*/*";
            } else
                mimeType = "*/*";
            intent.setDataAndType(path, mimeType);

            try {
                startActivity(intent);
            } catch (Exception e) {
                Toast.makeText(LoginSuccessActivity.this,
                        constants.Error_NoCompatibleApp, Toast.LENGTH_SHORT)
                        .show();
                e.printStackTrace();
            }
        } else {
            Toast.makeText(LoginSuccessActivity.this,
                    constants.Error_DocNotFound, Toast.LENGTH_SHORT).show();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

This is working fine, as I get app chooser pop-up for the filetypes which has multiple apps related to it(for eg. kingsoft office and polaris office for word file) OR directly file is opened for the filetypes which has only one app related to it(HTMLViewer for html file).

What I don't understand is my main activity is getting destroyed in few moments after calling file viewer activity. I've checked this by placing logs in onPause(), onStop() and onDestroy(). So, after file viewing is done, when I press back key, instead of taking back to app, it navigated to main menu.

I tried with startActivityForResult(intent, 1); instead of startActivity(intent); hoping that it'll preserve my main activity as it'll be waiting for result, but to no avail.

Please note that I've added android:configChanges="orientation|screenSize" in manifest file and

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

in java file.

Am I doing anything wrong?

Any help appreciated.

Sometimes, mind you, "Sometimes", I get follwing messages on log:

 file:// Uri exposed through Intent.getData()
 java.lang.Throwable: file:// Uri exposed through Intent.getData()
    at android.os.StrictMode.onFileUriExposed(StrictMode.java:1597)
    at android.net.Uri.checkFileUriExposed(Uri.java:2338)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:7194)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1418)
    at android.app.Activity.startActivityForResult(Activity.java:3423)
    at android.app.Activity.startActivityForResult(Activity.java:3384)
    at android.app.Activity.startActivity(Activity.java:3626)
    at android.app.Activity.startActivity(Activity.java:3594)
    at com.android.internal.app.ResolverActivity.onIntentSelected(ResolverActivity.java:407)
    at com.android.internal.app.ResolverActivity.startSelected(ResolverActivity.java:299)
    at com.android.internal.app.ResolverActivity.onButtonClick(ResolverActivity.java:289)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at android.view.View$1.onClick(View.java:3809)
    at android.view.View.performClick(View.java:4424)
    at android.view.View$PerformClick.run(View.java:18383)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4998)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
    at dalvik.system.NativeStart.main(Native Method)

Upvotes: 2

Views: 2694

Answers (2)

Sanket Shah
Sanket Shah

Reputation: 4382

Add these lines to your Intent and try to Start Activity,

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setType(Your_MimeType);

I m using this code in my App

String MimeType = URLConnection.guessContentTypeFromName(myFile.getAbsolutePath());

Intent intent = new Intent(android.content.Intent.ACTION_VIEW); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setType(MimeType);
Uri uri_path = Uri.fromFile(myFile);
intent.setDataAndType(uri_path, MimeType);
try
{
    _context.startActivity(intent);
}
catch(ActivityNotFoundException e)
{
    Toast.makeText(_context,"Activity Not Found..", Toast.LENGTH_SHORT).show();
}

Refer this link also for better understanding

Hope it will help you.

Upvotes: 1

Alexander
Alexander

Reputation: 48272

Activity can get destroyed any time. If someone has some data to retain one can do as follows:

class MyActivity {

private String mMyString;

public void onSaveInstanceState(Bundle b) {

b.putString("MYDATA", mMyString);
super.onSaveInstanceState(b);
}

public void onCreate(Bundle savedState) {
super.onCreate(savedState)
if(savedState != null) {
 mMyString = savedState.getString("MYDATA");
}

}

You can this way create your own subclass private static class State implements Serializable and save it in onSaveInstanceState()

Upvotes: 0

Related Questions