2012-02-27 9 views

答えて

1

は私には思える(また、いくつかのunasked質問に答える;)。)

同じのために、あなたは、

android.app.Applicationクラスを拡張することができます

グローバルアプリケーション状態を維持する必要があるユーザーのための基本クラス。 AndroidManifest.xmlのタグに名前を指定することで、独自の実装を提供することができます。これにより、アプリケーション/パッケージのプロセスが作成されたときにそのクラスがインスタンス化されます。Another question

public class MyApplication extends Application { 

    private String someVariable; 

    public String getSomeVariable() { 
     return someVariable; 
    } 

    public void setSomeVariable(String someVariable) { 
     this.someVariable = someVariable; 
    } 
} 

から


例次に、あなたの活動に、あなたはそうのような変数を取得して設定することができます例で与えられたよう

// set 
((MyApplication) this.getApplication()).setSomeVariable("foo"); 

// get 
String s = ((MyApplication) this.getApplication()).getSomeVariable(); 

セットあなたのVA 1つのアクティビティ(またはボタンは何でもクリックしてください)とは別のアクティビティでその値がになります。

+0

これは、新しいアクティビティを開くときに余分なものを追加するほうがどれほど優れているか分かりません。これは変数をメモリに保持します... –

1

オプション1:あなたがしたい活動のbroadcastReceiverを登録

Intent i = new Intent(); 
i.setAction(RESTART_CHAT_POLLING_INTENT); 
mContext.sendBroadcast(i); 

public static final String RESTART_CHAT_POLLING_INTENT = "com.cleeqa.android.chat.restart.intent"; 

放送それ: 送信アクティビティ(:[現在の例)でカスタム意図を作成します。受信(NewActivity):

BroadcastReceiver myReceiver = new MyBroadcastReceiver(); 
IntentFilter filter = new IntentFilter(); 
filter.addAction(CurrentActivity.RESTART_CHAT_POLLING_INTENT); 
registerReceiver(myReceiver, filter); 

は(またNewActivityで)放送を受信:

private class MyBroadcastReceiver extends BroadcastReceiver { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      if (intent.getAction().equals(CurrentActivity.RESTART_CHAT_POLLING_INTENT)) { 

       //DO WHAT YOU WANT 

      } 
    } 

オプション2(放送受信機の必要はありません): のonClick:のonCreate/ONSTARTあなたがしたい活動の

Intent intent = new Intent(CurrentActivity.this, NewActivity.class); 
intent.putExtra("PARAM", parameter); 
startActivity(intent); 

受信(NewActivity):

Bundle extras = getIntent().getExtras(); 
     if (extras != null) { 
      parameter = extras.getString("PARAM"); 
     } 
1

アプリケーションがブー変数をキャストしますか?現在のアクティビティから新しいアクティビティに変数を渡したければ、startActivityForResultを呼び出して現在のアクティビティから新しいアクティビティを開始し、その変数をパラメータとして渡します。

あなたが現在の活動使用startActivity

1

私はそれがintent.putExtra("PARAM", parameter);を使用して、彼が行う必要がある何のためにはるかに簡単かつ適切であるgetExtras()でそれを読んで渡す考えるに戻って結果を送信するために、新たな活動をしたくない場合は!あなたが活動全体で使用できるグローバル変数を、使用することができるように

関連する問題