-3

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.

Glave
  • 137
  • 1
  • 1
  • 10
  • in your `openMap()` method you are not putting anything into the Intent. You have to use my answer exactly as it is. – Murat Karagöz Nov 08 '16 at 15:24
  • @MuratK. I thought I did use your answer exatly as it is. Maybe I misunderstood you. So can you tell me what I am missing? Sorry, I am not very experienced. – Glave Nov 08 '16 at 15:28

3 Answers3

1

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);
}
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • Something doesnt work right. This is how understood it: ;`intent.putExtra("findLocation", 1)` and `int i = getIntent().getIntExtra("findlocation", 999) != 999;` On the last line Android Studio tells me "Incompatible Types. Required: Int, Found: Boolean". – Glave Nov 08 '16 at 14:56
  • @Glave oooops, remove the `!=` check. I was originally going to create a if statement. – Murat Karagöz Nov 08 '16 at 14:57
  • Okay, the activity starts now, but something still isnt right. My `setOnClickListener ` contains this code now: `openMap(cn.getID());`, but when I click on the button, the new activity tells me `i` is 999 (is there a special reason for that number?) instead of 1 and does not call the method. Also. I cant see right now, where my button ID is received right now in the new activity. If i `i == 1` it cannot be my button ID. – Glave Nov 08 '16 at 15:07
  • @Glave The method `getIntExtra(String, DefaultValue)` is built like that. If it can't find the key to the value it will return a default value in this case its 999. Where do you call `getIntent` and did you write the key correctly? – Murat Karagöz Nov 08 '16 at 15:09
  • I edited my post at the end. I am not sure if understand your question, so i wrote the code into it as it is right now. Maybe you can see then what is wrong with it. – Glave Nov 08 '16 at 15:20
  • Thank you it works now. You see, I didnt get that the 1 was just an example, also I made spelling mistake. The lowercase L in "findlocation". One last thing. It only works now, if my id is 1, but as it can be everything from 1 to n, my if statement in the oncreate method needs to be changed. Do you have any suggestion in what way? – Glave Nov 08 '16 at 15:43
  • @Glave change it to `if(i != 999)` so it will always go into the statement unless its the default value. – Murat Karagöz Nov 08 '16 at 15:46
  • Okay, I did that. Thank you for your patience. Do you have any idea why my question could have been downvoted two times? Maybe I made some obvious mistakes to you. I dont want to repeat them next time. – Glave Nov 08 '16 at 15:49
1

Step #1: Use putExtra() to add your ID value to the Intent that you use with startActivity()

Step #2: In the other activity, call getIntent() in onCreate() to retrieve the Intent used to create the activity instance. Call get...Extra() (where ... depends on the data type) to retrieve your ID value. If the ID value exists, call your findLocation() method.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

It depends on how you want it to work. If you only want it to execute when the activity is created (when it starts or screen rotates) then call the method within the activities onCreate method. If you want it called whenever the user returns to that activity which can include them leaving the app and coming back to it sometime later then onResume would be a better spot for it. Calling the method from either should work as you hope though.

I also recommend looking over the Activity lifecycle as that will help you a lot in the future.

CodyEngel
  • 1,501
  • 14
  • 22