2013-10-10 76 views
26

我試圖通過與活動使用意圖傳遞活動

我使用意圖定期活動

之間傳遞數據考慮如下::

AndroidTabRestaurantDescSearchListView.java

代碼中的數據之間的數據
public class AndroidTabRestaurantDescSearchListView extends TabActivity { 

    // TabSpec Names 
    private static final String INBOX_SPEC = "Rating"; 
    private static final String OUTBOX_SPEC = "Price"; 

    Button Photos; 
    Button Filter; 
    Button Search; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     TabHost tabHost = getTabHost(); 

     // Inbox Tab 
     TabSpec inboxSpec = tabHost.newTabSpec(INBOX_SPEC); 
     Intent inboxIntent = new Intent(this, RatingDescriptionSearchActivity.class); 
     inboxSpec.setIndicator(INBOX_SPEC); 
     // Tab Content 
     inboxSpec.setContent(inboxIntent); 

     // Outbox Tab 
     TabSpec PriceSpec = tabHost.newTabSpec(OUTBOX_SPEC); 
     Intent PriceIntent = new Intent(this, PriceDescriptionSearchActivity.class); 
     PriceSpec .setIndicator(OUTBOX_SPEC); 
     PriceSpec.setContent(PriceIntent); 


     // Adding all TabSpec to TabHost 
     tabHost.addTab(inboxSpec); 
     tabHost.addTab(PriceSpec); 

     //Set the current value tab to default first tab 
     tabHost.setCurrentTab(0); 

    } 

} 

假設我從Someother發送數據活動稱爲Activity-1AndroidTabRestaurantDescSearchListView的意圖

現在,我怎麼能接受的數據AndroidTabRestaurantDescSearchListView這是我從Activity-1了,然後再次將其傳遞給RatingDescriptionSearchActivity

Pictoral表示是 ::

enter image description here

{編輯} - 如果這是可能的基礎上的答案---歧義,因爲AndroidTabRestaurantDescSearchListView是一個標籤活動

TabSpec inboxSpec = tabHost.newTabSpec(INBOX_SPEC); 
     Intent inboxIntent = new Intent(this, RatingDescriptionActivity.class); 
     intent.putExtra("keyName", value); 
     inboxSpec.setIndicator(INBOX_SPEC); 
     // Tab Content 
     inboxSpec.setContent(inboxIntent); 
+0

看到這將幫助你http://stackoverflow.com/questions/18649728/android-cannot-pass-intent-extras-though-alarmmanager/18649849#18649849 –

回答

58

將數據從Activity-1AndroidTabRes..如下:

在發送活動......

Intent intent = new Intent(current.this, AndroidTabRestaurantDescSearchListView.class); 
intent.putExtra("keyName","value"); 
startActivity(intent); 

AndroidTabRes..活動......

String data = getIntent().getExtras().getString("keyName"); 

因此,您可以從發送活動接收活動有數據...

而在你AndroidTabRestaurantDescSearchListView類,這樣做:

String value= getIntent().getStringExtra("keyName"); 

Intent intent = new Intent(this, RatingDescriptionSearchActivity.class); 
intent.putExtra("keyName", value); 
startActivity(intent); 

然後在您的RatingDescriptionSearchActivity類,這樣做:

String data= getIntent().getStringExtra("keyName"); 
+0

請看更新的問題... ..我的含糊之處在於,因爲其中之一是標籤活動.......我是根據你的答案正確的方式..看看編輯 – Devrath

+0

那麼你的問題是什麼? – GrIsHu

+0

在編輯....我提到了傳遞數據的正確方式....因爲在tabactivity中......我們從不使用startActivity( - );所以...我很困惑! – Devrath

3

很簡單。

假設你Activity-1,你這樣做:

String stringExtra = "Some string you want to pass"; 

Intent intent = new Intent(this, AndroidTabRestaurantDescSearchListView.class); 

//include the string in your intent 
intent.putExtra("string", stringExtra); 

startActivity(intent); 

而在你AndroidTabRestaurantDescSearchListView類,這樣做:

//fetch the string from the intent 
String extraFromAct1 = getIntent().getStringExtra("string"); 

Intent intent = new Intent(this, RatingDescriptionSearchActivity.class); 

//attach same string and send it with the intent 
intent.putExtra("string", extraFromAct1); 
startActivity(intent); 

然後在您的RatingDescriptionSearchActivity類,這樣做:

String extraFromAct1 = getIntent().getStringExtra("string"); 
7

AndroidTabRestaurantDescSearchListView活動試試這個

Intent intent = new Intent(this,RatingDescriptionSearchActivity.class); 
intent.putExtras(getIntent().getExtras()); 
startActivity(intent); 

而且然後從RatingDescriptionSearchActivity活動只是打電話

getIntent().getStringExtra("key") 
+0

請看編輯過的問題....我的模棱兩可因爲中間活動是一個標籤活動 – Devrath

+1

答案仍然相同,您必須從第一個活動發送一個「意圖」到「TabActivity」,並且當您想要發射第三個您必須執行答案設置。 – xhamr

4

您可以使用捆綁來獲得數據:

Bundle extras = intent.getExtras(); 
String data = extras.getString("data"); // use your key 

而且又可以將此數據opass到下一個活動:

Intent intent = new Intent(this, next_Activity.class); 
    intent.putExtra("data", data); 
    startActivity(intent); 
+0

如果活動已經在運行,是否需要執行'startActivity(i);'?我的意思是,我可以讓* activity A * call * activity B *,並將數據返回給* activity A *?我困惑嗎? –

+1

您可以使用'StartActivityForResult'將結果返回給B,當它完成時從B返回。 –

+0

非常好,那個接縫很有用。你知道'StartActivityForResult'是否可以和AsynkTask一起使用嗎? –

2

主要活動

public class MainActivity extends Activity { 

    EditText user, password; 
    Button login; 

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

     user = (EditText) findViewById(R.id.username_edit); 
     password = (EditText) findViewById(R.id.edit_password); 
     login = (Button) findViewById(R.id.btnSubmit); 
     login.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(MainActivity.this,Second.class); 

       String uservalue = user.getText().toString(); 
       String name_value = password.getText().toString(); 
       String password_value = password.getText().toString(); 

       intent.putExtra("username", uservalue); 
       intent.putExtra("password", password_value); 
       startActivity(intent); 
      } 
     }); 
    } 
} 

次活動要在其中接收數據

public class Second extends Activity{ 

    EditText name, pass; 
    @Override 

    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.second_activity); 

     name = (EditText) findViewById(R.id.editText1); 
     pass = (EditText) findViewById(R.id.editText2); 

     String value = getIntent().getStringExtra("username"); 
     String pass_val = getIntent().getStringExtra("password"); 
     name.setText(value); 
     pass.setText(pass_val); 
    } 
} 
1

在FirstActivity:

Intent sendDataToSecondActivity = new Intent(FirstActivity.this, SecondActivity.class); 
sendDataToSecondActivity.putExtra("USERNAME",userNameEditText.getText().toString()); 
sendDataToSecondActivity.putExtra("PASSWORD",passwordEditText.getText().toString()); 
startActivity(sendDataToSecondActivity); 

在SecondActivity

onCreate()

String userName = getIntent().getStringExtra("USERNAME"); 
String passWord = getIntent().getStringExtra("PASSWORD"); 
相關問題