0
votes

I made an Activity to choose an app from the installed apps and get the launcher intent of the selected app and pass that intent back to an the starting Activity:

Intent intent = packageManager
                .getLaunchIntentForPackage(app.packageName);

        if (null != intent) {
            Intent data = new Intent();
            data.setData(Uri.parse(intent.toString()));
            setResult(RESULT_OK, data);
            finish();
        }

onActivityResult from the starting Activity:

public void onActivityResult(int requestCode, int resultCode, Intent data){
    if (requestCode == request_Code){
        if (resultCode == RESULT_OK){                   
            intent = data.getData().toString();     
            startActivity(new Intent(intent));
        }

I extracted intent String and the Intent inside startActivity from Logcat and I get this:
intentString:Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.android.providers.downloads.ui cmp=com.android.providers.downloads.ui/.DownloadsListTab }
Intent inside startActivity: Intent { act=Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.android.providers.downloads.ui cmp=com.android.providers.downloads.ui/.DownloadsListTab } }
(My app crashed because of this wrong Intent)
As you can see, the String I passed back to the starting Activity is the Intent I need itself(in String)
So is there anyway I can make that String an Intent without Intent's Constructor? Or maybe a way to pass Intent directly to the starting Activity?

2

2 Answers

0
votes

It looks like you're making it "too complicated"... You're trying to use a String to start an Activity, not an Intent. Instead, simply pass the Intent you want to use. For example, try this:

Intent intent = packageManager
            .getLaunchIntentForPackage(app.packageName);

    if (null != intent) {
        setResult(RESULT_OK, intent);
        finish();
    }

And then do this:

public void onActivityResult(int requestCode, int resultCode, Intent data){
    if (requestCode == request_Code){
        if (resultCode == RESULT_OK){                   
            startActivity(data);
        }
    }
}
2
votes

I think you are trying to pass a string to other activity , You need to pass it in extras.

String messageToPass= "Hello Word!";

Intent intent = new Intent(this, toClassName.class); intent.putExtra("message", messageToPass); startActivity(intent);

and then extract in other activity , like

Intent intent = getIntent(); String messageReceived= intent.getExtras().getString("message");