简体   繁体   English

ANDROID:从2个不同的Activity(不同的Intent)开始相同的Activity

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

I have a little problem: I have 3 Activity. 我有一点问题:我有3个活动。 Activity ListClass.java 活动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;
}

As you can see I putExtra values that I use on ClassB.java : 正如您所看到的,我在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);

    }
}

SO FAR SO GOOD! 到现在为止还挺好! But I need to start ActivityB from another activity that is MainClass.java. 但我需要从另一个MainClass.java活动启动ActivityB。

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

            startActivity(intent);

the problem is that when ClassB uses Bundle bundle = getIntent().getExtras(); 问题是ClassB使用Bundle bundle = getIntent().getExtras(); he doesn't find "extras" because Intent of MainClass is different from Intent of ListClass! 他没有找到“额外”,因为MainClass的Intent与ListClass的Intent不同! how can I do?? 我能怎么做?? can I decide which Intent use? 我可以决定使用哪种Intent吗? tahnk you! tahnk你!

EDIT: @skygeek ok I'm working on it, but how can I create a static variable if the extra values change? 编辑:@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);
}

Make a check inside ClassB's onCreate method 在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
      }

create an static object with static field intent when calling the activity for first time(check if its null). 在第一次调用活动时创建具有静态字段意图的静态对象(检查其是否为null)。 So next time you call the same activity from another class use this same static object having the already set intent to call the activity again and hence you will find those extras! 因此,下次从另一个类调用相同的活动时,请使用具有已设置意图的相同静态对象再次调用该活动,因此您将找到这些额外内容!

This is wrong: 这是错的:

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;  

} }

You create new intent after each function call getIntent()... Try this: 在每次函数调用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);

I understand... try this: 我理解......试试这个:

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