1

2つのアプリケーションがあります。最初のアプリケーションのユーザーは、彼ができる行動の2つのオプションがあるダイアログを開くことができます。これは次のように行われる:2つのアプリケーション間でインテントを連続的に処理する

public void sendSms(String number) 
{ 
    Intent myIntent = new Intent(Intent.ACTION_SENDTO); 
    myIntent.setData(Uri.parse("smsto:" + number); 
    startActivity(myIntent); 
} 

public void call(String number) 
{ 
    Intent myIntent = new Intent(Intent.ACTION_CALL); 
    myIntent.setData(Uri.parse("tel:" + number); 
    startActivity(myIntent); 
} 

第二アプリはインテントフィルタを有しており、それはこのアクションを識別する:

<activity android:name="com.my.path.MyActivity" 
      android:label="@string/app_name" 
      android:configChanges="orientation" 
      android:screenOrientation="portrait" 
      android:launchMode="singleTask" > 


<intent-filter> 
     <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 

     <action android:name="android.intent.action.CALL" /> 
     <action android:name="android.intent.action.CALL_BUTTON" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <data android:scheme="tel" /> 

    </intent-filter> 

    <intent-filter> 

     <action android:name="android.intent.action.SENDTO" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <data android:scheme="smsto" /> 
    </intent-filter> 

    <intent-filter android:priority="100" > 

     <action android:name="android.intent.action.DIAL" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <data android:scheme="tel" /> 

    </intent-filter> 

    <intent-filter> 

     <action android:name="android.intent.action.CALL_BUTTON" /> 
     <category android:name="android.intent.category.DEFAULT" /> 

    </intent-filter> 

    <intent-filter> 

     <action android:name="android.intent.action.CALL_PRIVILEGED" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <data android:scheme="tel" /> 

    </intent-filter> 

    <intent-filter> 

     <action android:name="android.intent.action.VIEW" /> 
     <action android:name="android.intent.action.DIAL" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <category android:name="android.intent.category.BROWSABLE" /> 
     <data android:scheme="tel" /> 

    </intent-filter>  
</activity> 

第二のアプリケーションがこのようなインテントを処理する:

protected void onNewIntent(Intent inten) 
{ 
    super.onNewIntent(intent); 
    String action = intent.getAction(); 
    if(action.equals(Intent.ACTION_CALL)) 
    { 
     *here make a fragment transaction to the fragment which handles calls * 
    } 
    else if(action.equals(Intent.ACTION_SENDTO)) 
    { 
     *here make a fragment transaction to the fragment which handles sms* 

    } 
} 

ユーザーが最初のアプリケーションを開き、アクションの1つを選択すると、それは正常に機能し、戻るボタンを押すと、彼は最初のアプリケーションに戻ります。彼が最初のアプリケーションに戻ったとき、ダイアログはまだアクションをしたときのように開いています。

問題は、ユーザーが2番目のアプリケーションを呼び出すためにコールアクションを再度押すと、2番目のアプリケーションがフォアグラウンドになりますが、onNewIntentが再び呼び出されたように見えないため、フラグメントトランザクションは起こる。

次に、2回目にonCreateメソッドが呼び出されたので、そこにインテントを処理しようとしました。 SENDTOアクションでは機能しましたが、CALLアクションでは機能しませんでした。 CALLアクションでは、onCreateインテント処理が呼び出されていますが、何らかの理由でフラグメントトランザクションが機能しません(SENDTOアクションのフラグメントトランザクションが機能します)。

同じアクションを再度実行しようとすると、onNewIntentメソッドが呼び出されないのはなぜですか?そして、コール・アクションのためにトランザクションが機能しないのはなぜですか?

UPDATE:フラグメントトランザクションは、今すぐonCreateメソッドで機能します。しかし、私はまだすべてを処理することを好むだろうonNewIntent

+0

あなたは 'finish()'を呼び出していますか?アプリがクラッシュしているかどうかをlogcatで調べましたか? Androidは 'Activity'の新しいインスタンスを作成した場合にのみonCreate()を呼び出します。すでにインスタンスがアクティブ(生きている)の場合、Androidは新しいインスタンスを作成しません。 –

+0

アプリはクラッシュせず、私はfinish()を呼び出しません。アプリは、もう一方がインテントを送信するまで、バックグラウンドにあります。 –

答えて

1

これはアンドロイドフレームワークによって決定され、あなたはアクティビティを観察することによってそれをテストすることができます。最初にアプリケーションを起動すると、onCreate関数が呼び出されますが、onNewIntentは呼び出されません。ホームメニューをクリックすると、アクティビティはバックグラウンドで動作します。今度は再び同じ意図を開始しますが、onNewIntentが呼び出されますが、onCreateは呼び出されません。

私はお勧めします:onCreateonNewIntentにインテント(Uri uri = intent.getData())のデータを保存するだけです。 onResume機能では、得意先のデータを処理します。

お手数ですが、

+0

しかしこれはそうではありません..私はすでに実行中のアプリを受け取る意図について話しています。次に、インテントを取得し、onNewIntentが呼び出されます。その後、別の意図があり、突然onNewIntentは呼び出されませんが、onCreateは呼び出されます。 onNewIntentが呼び出された後初めてonNewIntentが呼び出されないのはなぜですか? –

+0

アクティビティがシステムによって殺された可能性がありますので、再度onCreateを呼び出します。私の提案する方法では、onCreateまたはonNewIntent呼び出しに関係なく、インテントのデータを保存するだけです。とにかく、それは1と呼ばれなければならない。 – yanzi1225627

+0

データを保存する必要はありません。フラグメントトランザクションを作成するだけです。私が書いたように、最初のインテントはonNewIntentを呼び出すことで、onCreateが常に呼び出されることに頼ることができないかどうかわからないので、2番目のインテントも同様にしたいだけです。 –

関連する問題