在createChooser之后启动其他活动

时间:2017-12-20 01:33:51

标签: android

如果createChooser启动的应用程序(如下面的show)已完成,您如何重定向到另一个活动?

我的尝试最终会在createChooser启动之前触发第二个意图。我注意到,当我按下新启动的活动上的后退按钮时,createChooser出现在我想要启动它的活动上。

我还尝试将createChooser包装在startActivityForResult中,然后使用onActivityResult启动第二个intent,但结果是相同的

startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Intent trackIntent = new Intent(InformContacts.this, TrackOffers.class);
startActivity(trackIntent);

以下是整个代码:

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class InformContacts extends Activity {

private static String ITEM_NAME = "Item name";
private static String ITEM_PRICE = "Item price";
private static String ITEM_PIC_URI = "Item pic uri";
public static final int REQUEST_SEND_EMAIL = 0;

ArrayList<Contact> listContacts;
ListView lvContacts;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_inform_contacts);

    listContacts = new ContactFetcher(this).fetchAll();
    lvContacts = (ListView) findViewById(R.id.lvContacts);
    final ContactsAdapter adapterContacts = new ContactsAdapter(this, listContacts);
    lvContacts.setAdapter(adapterContacts);

    final Button informButton = (Button) findViewById(R.id.button6);

    final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    final String uriSharedPref = preferences.getString(ITEM_PIC_URI, "item pic uri");

    informButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            ArrayList emailsList = adapterContacts.emailsSelected;
            String item = preferences.getString(ITEM_NAME, "item name");
            Float price = preferences.getFloat(ITEM_PRICE,0);

            //May use the uriSharedPref string to embed actual url in email and show recipients like I did with publishing house
            String sellingMessage = "Hello,\n\nI'm selling my "+ item +" for KES "+price+".\n\nGet back to me if you're interested in buying.\n\n" + uriSharedPref;
            String subject = "Selling my "+item;

            sendEmail(emailsList, sellingMessage, subject);

            //Intent trackIntent = new Intent(InformContacts.this, TrackOffers.class);
            //startActivity(trackIntent);
        }
    });
}

protected void sendEmail(ArrayList<String> arrayOfEmails, String message, String subject) {

    Log.i("Send email", "");
    Intent emailIntent = new Intent(Intent.ACTION_SEND);

    //First Step: convert ArrayList to an Object array
    Object[] objEmails = arrayOfEmails.toArray();
    //Second Step: convert Object array to String array
    String[] strEmails = Arrays.copyOf(objEmails, objEmails.length, String[].class);

    emailIntent.setData(Uri.parse("mailto:"));
    emailIntent.setType("text/plain");
    emailIntent.putExtra(Intent.EXTRA_EMAIL, strEmails);
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
    emailIntent.putExtra(Intent.EXTRA_TEXT, message);

    PackageManager packageManager = getPackageManager();
    List activities = packageManager.queryIntentActivities(emailIntent, PackageManager.MATCH_DEFAULT_ONLY);
    boolean isIntentSafe = activities.size() > 0;

    if(isIntentSafe){

        startActivityForResult(Intent.createChooser(emailIntent, "Send mail..."), REQUEST_SEND_EMAIL);
        //finish();
        Log.i("Done sending email...", "");
    }
    else{

        Toast.makeText(InformContacts.this, "No email app found. Email won't be sent.", Toast.LENGTH_SHORT).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){

    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK){

        if(requestCode == REQUEST_SEND_EMAIL){

            Intent intent = new Intent(this, TrackOffers.class);
            startActivity(intent);
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

试试这个

更改为:

    if(requestCode == REQUEST_SEND_EMAIL){

        Intent intent = new Intent(this, TrackOffers.class);
        startActivity(intent);
    }

而不是:

 if (resultCode == RESULT_OK){

    if(requestCode == REQUEST_SEND_EMAIL){

        Intent intent = new Intent(this, TrackOffers.class);
        startActivity(intent);
    }
}