Search code examples
javaandroidandroid-intentosmdroid

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


As soon as the setOnClickListener executes I want to start another activity and transmit the variable cn.getID() to it. When inside the other activity I want to immidietaly start the method findlocation and give cn.getID() to it.

The method findLocation is not finished yet. The idea is, once it gets the ID of the other activities button, i can search with sqllite in my database for the place it belongs to, get longitude and latitude and tell mapcontroller focus the world map on this point.

@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);
}

This is the method from inside the new activity I want to execute immidietaly after it has started:

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);
}

EDIT: @Murat K. After some edits this is my whole class now:

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);
}

}

And this is the getIntent of my onCreate method in UI_MainActivity:

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

As I edited into my earlier comment, i cant see where my Button-ID is recieved. At first i thought i would be my ID, but that wouldnt work, since The Button ID can be every number from 1 to n.


Solution

  • You can achieve this with an Intent e.g.

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

    and in your onCreate() method of the starting Activity you check for it.

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

    EDIT:

    //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);
    }