2017-12-20 21 views
0

Wie können Sie zu einer anderen Aktivität umleiten, nachdem die von createChooser gestartete Anwendung (z. B. show) vollständig ist?Andere Aktivität nach createChooser starten

Der folgende Versuch endet damit, dass die zweite Absicht ausgelöst wird, bevor createChooser gestartet wird. Ich habe festgestellt, dass, wenn ich die Zurück-Taste auf der neu gestarteten Aktivität drücke, der CreateChooser auf der Aktivität erscheint, von der ich ihn starten wollte.

Ich habe auch versucht die createChooser in startActivityForResult zu wickeln und dann die zweite Absicht starten mit onActivityResult aber war das Ergebnis das gleiche

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

Hier ist der gesamte Code:

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); 
     } 
    } 
} 
} 
+0

Sicherlich wird es in der startActivityResult arbeiten, wenn u nächste Aktivität() aufrufen Sie Code – R2R

+0

Hallo @ R2R teilen, ich habe meine Frage mit dem vollständigen Code aktualisiert. –

Antwort

0

Versuchen Sie, diese

Wechseln zu:

if(requestCode == REQUEST_SEND_EMAIL){ 

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

statt.

if (resultCode == RESULT_OK){ 

    if(requestCode == REQUEST_SEND_EMAIL){ 

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

Danke R2R. Das hat funktioniert. Frohes Neues Jahr übrigens Was hat dich zum Arbeiten gebracht? –

Verwandte Themen