如何启动另一个 activity 并在其中启动一个方法?

How to start another activity and start a method inside it?

一旦 setOnClickListener 执行,我想启动另一个 activity 并将变量 cn.getID() 传输给它。当在另一个 activity 中时,我想立即启动方法 findlocation 并给它 cn.getID()

方法findLocation尚未完成。这个想法是,一旦它获得其他活动按钮的 ID,我就可以在我的数据库中使用 sqllite 搜索它所属的地方,获取经度和纬度并告诉 mapcontroller 将世界地图聚焦在这点。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.verladestellen);
    final DB_Verladestellen db = new DB_Verladestellen(this);
    List<DB_Place> placeList = db.getAllDBPlaces();
    final LinearLayout layout = (LinearLayout) findViewById(R.id.verladestellen_liste);

    for (final DB_Place cn : placeList) {

        final LinearLayout row = new LinearLayout(this);
        row.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        Button place = new Button(this);
        place.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        place.setText(cn.getName());
        place.setId(cn.getID());
        row.addView(place);
        row.setId(cn.getID());

        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) place.getLayoutParams();
        params.weight = 1.0f;
        place.setLayoutParams(params);

        place.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Here I want to call the method to start the other activity 
                //and transmit cn.getID(). 
                openMap(null); 
            }
        });

        layout.addView(row);
    }
}

//The method to start the other activity
public void openMap(View view) {
    Intent intent = new Intent(this, UI_MainActivity.class);
    startActivity(intent);
}

这是新的方法 activity 我想在它启动后立即执行:

public void findLocation(View view){
    MapView map = (MapView) findViewById(R.id.map);
    IMapController mapController = map.getController();
    mapController.setZoom(17);
    GeoPoint myLocation = new GeoPoint(PLACEHOLDER X  , PLACEHOLDER Y);
    mapController.animateTo(myLocation);
}

编辑: @Murat K。经过一些编辑,这是我现在的全部 class:

public class UI_Verladestellen extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.verladestellen);
    final DB_Verladestellen db = new DB_Verladestellen(this);
    List<DB_Place> placeList = db.getAllDBPlaces();
    final LinearLayout layout = (LinearLayout) findViewById(R.id.verladestellen_liste);

    for (final DB_Place cn : placeList) {

        final LinearLayout row = new LinearLayout(this);
        row.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        Button place = new Button(this);
        place.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        place.setText(cn.getName());
        place.setId(cn.getID());
        row.addView(place);
        row.setId(cn.getID());

        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) place.getLayoutParams();
        params.weight = 1.0f;
        place.setLayoutParams(params);

        place.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openMap(cn.getID());
            }
        });

        layout.addView(row);
    }
}

public void openMap(int view) {
    Intent intent = new Intent(UI_Verladestellen.this, UI_MainActivity.class);
    intent.putExtra("findLocation", 1);
    startActivity(intent);
}

}

这是我在 UI_MainActivity 中的 onCreate 方法的 getIntent:

int i = getIntent().getIntExtra("findlocation", 999);
    if(i == 1){
        findLocation(i);
    }

当我编辑我之前的评论时,我看不到我的 Button-ID 是从哪里收到的。起初我以为我会是我的 ID,但这行不通,因为按钮 ID 可以是从 1 到 n 的每个数字。

您可以使用 Intent 例如

 Intent i = new Intent(BaseActivity.this, YourSecondActivity.class);
 intent.putExtra("METHOD_TO_CALL", 1);
 startActivity(i);

并在您的 onCreate() 开始方法中检查 Activity

@Override
  public void onCreate(Bundle savedInstanceState) {
    int i = getIntent().getIntExtra("METHOD_TO_CALL", 999);
    if(i == 1){
      callMethod(i);
    }

编辑:

//The method to start the other activity
public void openMap(View view) {
    Intent intent = new Intent(this, UI_MainActivity.class);
    intent.putExtra("METHOD_TO_CALL", 1); // the 1 is a example, put your ID here
    startActivity(intent);
}

第 1 步:使用 putExtra() 将您的 ID 值添加到与 startActivity()

一起使用的 Intent

步骤 #2:在另一个 activity 中,调用 onCreate() 中的 getIntent() 以检索用于创建 activity 实例的 Intent。调用 get...Extra()(其中 ... 取决于数据类型)以检索您的 ID 值。如果 ID 值存在,请调用您的 findLocation() 方法。

这取决于您希望它如何工作。如果您只希望它在创建 activity 时执行(当它启动或屏幕旋转时),则在活动 onCreate 方法中调用该方法。如果您希望它在用户 returns 到那个 activity 时调用它,这可以包括他们离开应用程序并在稍后的某个时间返回它,那么 onResume 将是一个更好的选择。不过,从其中任何一个调用方法应该会如您所愿。

我还建议您查看 Activity 生命周期,因为这对您将来有很大帮助。