2017-12-20 11 views
0

下記のようにcreateChooserによって起動されたアプリケーションが完了したら、どのように別のアクティビティにリダイレクトできますか?createChooserの後に他のアクティビティを起動します

私の試みでは、createChooserが起動する前に2番目のインテントがトリガーされてしまいます。新しく起動されたアクティビティの[戻る]ボタンを押すと、起動したいアクティビティにcreateChooserが表示されることに気付きました。

私はまた、startActivityForResultでcreateChooserをラップして、onActivityResult用いて第2の意図を起動しようとしましたが、結果は同じ

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

uは次のアクティビティを呼び出す場合、それはstartActivityResultに動作しますが、()あなたのコード – R2R

+0

こんにちは@ R2Rを共有し、私は完全なコードで私の質問を更新しました。 –

答えて

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

ありがとうR2R。それはうまくいった。あなたの作品は何のために働いたのですか? –

関連する問題