簡體   English   中英

ANDROID:從2個不同的Activity(不同的Intent)開始相同的Activity

[英]ANDROID: Start same Activity from 2 different Activity (different Intent)

我有一點問題:我有3個活動。 活動ListClass.java

public class ListClass extends Activity {

......


listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            onItemClick1(parent, view, position, id);

        }

    });

}

public void onItemClick1(AdapterView<?> parent, View view, int position,
        long id) {

    String name = (String) parent.getItemAtPosition(position);
    String address = (String) listAddress.get(position);
    int status = (int) listStatus.get(position);
    double lat = (double) listLat.get(position);
    double lng = (double) listLng.get(position);
    String serialNumber = (String) listSerialNumber.get(position);

    getIntent().putExtra("name", name);
    getIntent().putExtra("address", address);
    getIntent().putExtra("status", status);
    getIntent().putExtra("latitude", lat);
    getIntent().putExtra("longitude", lng);
    getIntent().putExtra("serialNumber", serialNumber);

    startActivity(getIntent());

}

public Intent getIntent() {
    Intent intent = new Intent(ListClass.this, ClassB.class);
    return intent;
}

正如您所看到的,我在ClassB.java上使用了putExtra值:

public class ClassB extends Activity {

TextView textViewTitle;
TextView textView2;
TextView textViewInfo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.charge_box);
    this.textViewTitle = (TextView) findViewById(R.id.charge_box__textView1);
    this.textView2 = (TextView) findViewById(R.id.charge_box__textView2);
    this.textViewInfo = (TextView) findViewById(R.id.charge_box__textView3);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    Bundle bundle = getIntent().getExtras();
    String name = bundle.getString("name");
        String address = bundle.getString("address");
        int status = bundle.getInt("status");
        textViewTitle.setText(name);
        textViewInfo.setText(address + " " + status);

    }
}

到現在為止還挺好! 但我需要從另一個MainClass.java活動啟動ActivityB。

    Intent intent = new Intent(MainActivity.this,
                    ClassB.class);

            startActivity(intent);

問題是ClassB使用Bundle bundle = getIntent().getExtras(); 他沒有找到“額外”,因為MainClass的Intent與ListClass的Intent不同! 我能怎么做?? 我可以決定使用哪種Intent嗎? tahnk你!

編輯:@skygeek好我正在研究它,但如果額外的值改變,我怎么能創建一個靜態變量?

public void onItemClick1(AdapterView<?> parent, View view, int position,
        long id) {

    String name = (String) parent.getItemAtPosition(position);
    String address = (String) listAddress.get(position);
    int status = (int) listStatus.get(position);
    double lat = (double) listLat.get(position);
    double lng = (double) listLng.get(position);
    String serialNumber = (String) listSerialNumber.get(position);

    Intent intent = new Intent(ListClass.this, ChargeBoxInfoActivity.class);
    intent.putExtra("name", name);
    intent.putExtra("address", address);
    intent.putExtra("status", status);
    intent.putExtra("latitude", lat);
    intent.putExtra("longitude", lng);
    intent.putExtra("serialNumber", serialNumber);
    startActivity(intent);
}

在ClassB的onCreate方法中進行檢查

     if(getIntent().getExtras() != null)
     {
     // do your functionality with extras
        Bundle bundle = getIntent().getExtras();
        String name = bundle.getString("name");
        String address = bundle.getString("address");
        int status = bundle.getInt("status");
        textViewTitle.setText(name);
        textViewInfo.setText(address + " " + status);
     }

     else
      {
       // do whatever you want to do
      }

在第一次調用活動時創建具有靜態字段意圖的靜態對象(檢查其是否為null)。 因此,下次從另一個類調用相同的活動時,請使用具有已設置意圖的相同靜態對象再次調用該活動,因此您將找到這些額外內容!

這是錯的:

getIntent().putExtra("name", name);
getIntent().putExtra("address", address);
getIntent().putExtra("status", status);
getIntent().putExtra("latitude", lat);
getIntent().putExtra("longitude", lng);
getIntent().putExtra("serialNumber", serialNumber);
startActivity(getIntent());

}

public Intent getIntent() {
Intent intent = new Intent(ListClass.this, ClassB.class);
return intent;  

}

在每次函數調用getIntent()之后創建新的意圖...試試這個:

Intent intent = new Intent(ListClass.this, ClassB.class);
intent.putExtra("name", name);
intent.putExtra("address", address);
intent.putExtra("status", status);
intent.putExtra("latitude", lat);
intent.putExtra("longitude", lng);
intent.putExtra("serialNumber", serialNumber);
startActivity(intent);

我理解......試試這個:

try{
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("name");
    String address = bundle.getString("address");
    int status = bundle.getInt("status");
    textViewTitle.setText(name);
    textViewInfo.setText(address + " " + status);
}}catch (Exception ignored) {
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM