Well, I was watching this tutorial:http://www.youtube.com/watch?v=9ew_Ajpqwqg#t=5m25s
Here you can see that he writes Intent i = new Intent......
But my app doesn't work like that :/
Here's my code
MainActivity.java
public class MainActivity extends Activity implements OnClickListener {
Button btnAddCategory, btnViewCategory;
EditText txtCategories;
TextView viewMain, viewAllCategories;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtCategories = (EditText) findViewById(R.id.txtCategories);
viewMain = (TextView) findViewById(R.id.viewMain);
btnAddCategory = (Button) findViewById(R.id.btnAddCategory);
btnViewCategory = (Button) findViewById(R.id.btnViewCategory);
viewAllCategories = (TextView) findViewById(R.id.viewAllCategories);
btnAddCategory.setOnClickListener(this);
btnViewCategory.setOnClickListener(this);
}
public void onClick(View arg0) {
switch(arg0.getId())
{
case R.id.btnAddCategory:
boolean didItWork = true;
try {
Categories entry = new Categories(MainActivity.this);
entry.open();
entry.createEntry(txtCategories.getText().toString());
entry.close();
} catch(Exception e) {
didItWork = false;
} finally {
if (didItWork)
{
Dialog d = new Dialog(MainActivity.this);
d.setTitle("New Category Created");
TextView tv = new TextView(MainActivity.this);
tv.setText("Success");
d.setContentView(tv);
d.show();
}
}
break;
case R.id.btnViewCategory:
Intent i = new Intent("android.intent.action.SQLVIEW");
startActivity(i);
break;
}
}
}
SQLView.Java
public class SQLView extends Activity {
protected void OnCreate(Bundle savedInstanceSatate) {
super.onCreate(savedInstanceSatate);
setContentView(R.layout.sqlview);
TextView tv = (TextView) findViewById(R.id.infosFromDb);
Categories info = new Categories(this);
info.open();
String data = info.getData();
info.close();
tv.setText(data);
} }
Manifest
<activity
android:name=".SQLView" android:label="#string/app_name">
<action android:name="android.intent.action.SQLVIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</activity>
I've read a thread here, where somebody was having the same problem, so I changed it to:
case R.id.btnViewCategory:
Intent i = new Intent(MainActivity.this, SQLView.class);
startActivity(i);
break;
So, now I don't get any exceptions, but I get an empty view. I can only see the name of my app, but that's it, all the TextViews etc. in the views are gone.
Is there anyone with an idea how to handle this?
Thanks in advance
Most of the times we write intents ,we miss some thing ,which takes time
I don't have Solution to your problem but i think its better than finding the missing part by watching the youtube video again and again.
Right Click on Project name for example : for Myandroidprojext
Myandroidprojext->new->other->Android Activity
This is the easy way to make new Activity without changing the Manifest Files or doing extrawork.
Related
For some reason my buttons aren't doing anything. I've used this method to implement buttons before and it never gave me an issue. The app has seven different buttons that all move to a different activity.
public class ScheduleActivity extends AppCompatActivity implements View.OnClickListener {
private Button mondayButton,tuesdayButton,wednesdayButton,thursdayButton,fridayButton,saturdayButton,sundayButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
mondayButton = findViewById(R.id.monday_button);
tuesdayButton = findViewById(R.id.tuesday_button);
wednesdayButton = findViewById(R.id.wednesday_button);
thursdayButton = findViewById(R.id.thursday_button);
fridayButton = findViewById(R.id.friday_button);
saturdayButton = findViewById(R.id.saturday_button);
sundayButton = findViewById(R.id.sunday_button);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.monday_button:
Intent monday_intent = new Intent(ScheduleActivity.this, MondayActivity.class);
startActivity(monday_intent);
break;
case R.id.tuesday_button:
Intent tuesday_intent = new Intent(ScheduleActivity.this, TuesdayActivity.class);
startActivity(tuesday_intent);
break;
case R.id.wednesday_button:
Intent wednesday_intent = new Intent(ScheduleActivity.this, WednesdayActivity.class);
startActivity(wednesday_intent);
break;
case R.id.thursday_button:
Intent thursday_intent = new Intent(ScheduleActivity.this, ThursdayActivity.class);
startActivity(thursday_intent);
break;
case R.id.friday_button:
Intent friday_intent = new Intent(ScheduleActivity.this, FridayActivity.class);
startActivity(friday_intent);
break;
case R.id.saturday_button:
Intent saturday_intent = new Intent(ScheduleActivity.this, SaturdayActivity.class);
startActivity(saturday_intent);
case R.id.sunday_button:
Intent sunday_intent = new Intent(ScheduleActivity.this, SundayActivity.class);
startActivity(sunday_intent);
}
}
}
You are getting the instances of the buttons but never setting an OnClickListener for them. You need to set the click listener for the buttons:
mondayButton.setOnClickListener(this)
You need to do this to all buttons, this tells your code where to notify the event when the button is clicked.
you are not attaching the listener View.OnClickListener to any of your buttons.
add this in your onCreate() after you init your buttons, your buttons will work
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
...
sundayButton = findViewById(R.id.sunday_button);
// attaching listeners
mondayButton.setOnClickListener(this);
tuesdayButton.setOnClickListener(this);
wednesdayButton.setOnClickListener(this);
thursdayButton.setOnClickListener(this);
fridayButton.setOnClickListener(this);
saturdayButton.setOnClickListener(this);
sundayButton.setOnClickListener(this);
}
You have to set the view.setOnClickListener{} on the OnCreate method
private Button mondayButton,tuesdayButton,wednesdayButton,thursdayButton,fridayButton,saturdayButton,sundayButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
mondayButton = findViewById(R.id.monday_button);
tuesdayButton = findViewById(R.id.tuesday_button);
wednesdayButton = findViewById(R.id.wednesday_button);
thursdayButton = findViewById(R.id.thursday_button);
fridayButton = findViewById(R.id.friday_button);
saturdayButton = findViewById(R.id.saturday_button);
sundayButton = findViewById(R.id.sunday_button);
mondayButton.setOnClickListener() {
Intent intent = new Intent(...) ;
startActivity(intent) ;
}
}
The app has a MainActivity with 6 editText fields, and a button. There are 5 more activities, named Activity2, Activity3, etc. Now, When a user enters names in editText fields, and press a button, the app should find out how many editText fields are filled, and open the activity with a corresponding number in it's name.
Example:
If only one field is filled, a toast should appear, saying More players.
If two fields are filled, app opens Activity2.
If three fields are filled, app opens Activity3, etc.
Now, to the problem. I am missing something out, and can't find out what. Here is MainActivity.java
public class MainActivity extends AppCompatActivity {
private EditText editText1,editText2,editText3,editText4,editText5,editText6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn);
editText1 = findViewById(R.id.editText1);
editText2 = findViewById(R.id.editText2);
editText3 = findViewById(R.id.editText3);
editText4 = findViewById(R.id.editText4);
editText5 = findViewById(R.id.editText5);
editText6 = findViewById(R.id.editText6);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int filledFileds = countFilledFields();
Log.d("filled", String.valueOf(filledFileds));
Class newClass = MainActivity.class;
switch (filledFileds){
case 1:
Context context = getApplicationContext();
CharSequence text = "You need more players!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
break;
case 2:
newClass = Activity2.class;
System.out.println("Activity2");
break;
case 3:
newClass = Activity3.class;
System.out.println("Activity3");
break;
case 4:
newClass = Activity4.class;
System.out.println("Activity4");
break;
case 5:
newClass = Activity5.class;
System.out.println("Activity5");
break;
case 6:
newClass = Activity6.class;
System.out.println("Activity6");
break;
default:
}
Intent intent = new Intent(MainActivity.this, newClass);
}
});
}
private int countFilledFields() {
ArrayList<EditText> editTexts = new ArrayList<>();
editTexts.add(editText1);
editTexts.add(editText2);
editTexts.add(editText3);
editTexts.add(editText4);
editTexts.add(editText5);
editTexts.add(editText6);
int filledNumber = 0;
for(int i = 0;i < editTexts.size() ;i++){
if(editTexts.get(i).getText()!=null && !editTexts.get(i).getText().toString().matches("")){
filledNumber += 1;
}
}
return filledNumber;
}
}
The log shows the exact number, something is not working...
Here is your click listener, with the switch omitted for brevity:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int filledFileds = countFilledFields();
Log.d("filled", String.valueOf(filledFileds));
Class newClass = MainActivity.class;
switch (filledFileds){
...
}
Intent intent = new Intent(MainActivity.this, newClass);
}
The problem is at the very end: you've created an Intent object ... but you're not doing anything with it. Probably you have just forgotten a startActivity() call:
Intent intent = new Intent(MainActivity.this, newClass);
startActivity(intent);
Also, looking this over, you have a problem with the case where the user only enters one EditText. As written, you'll still try to start a new activity (you'll just start a new copy of the same MainActivity, which is probably a bad idea). A better idea would be to only start the new activity if the user fills out enough EditTexts:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int filledFileds = countFilledFields();
Log.d("filled", String.valueOf(filledFileds));
Class newClass = null;
switch (filledFileds){
...
}
if (newClass != null) {
Intent intent = new Intent(MainActivity.this, newClass);
startActivity(intent);
}
}
You're missing one thing:
startActivity(intent);
I have about 50 activities in my app and I have an algorithm which displays the title of like 10 of those activities in the form of buttons in a super activity and sets an onclicklistener to each button which contains an intent and it calls the specific activity. I tried to do this via an array of intents but I got no success. Any suggestions on how I can perform this?
package plkk.developers.com.livfit;
// this is my string which contains name of activities
final String ActivityIdMen[] = { "Deadlift", "Pushups", "Barbell_Bench", "Military_Press", "Barbell_Curl", "Close_Bench", "Seated_Cable", "Chinup", "Overhead_Press",
"Power_Clean", "Jumping_Rope", "Hiit", "Barbell_Bench", "Deadlift", "Lat_Pulldown", "Barbell_Curl", "Skull_Crusher", "Diamond_Dips", "Squats",
"Hill_Running", "Jumping_Rope", "Stationary_Bike", "Hiit", "Chinup", "Torso_Rotation", "Prone_Plank", "Medicine_Squat", "Front_Squat"
};
// this is a fragment of the algorithm where I need help
if(BMI<18.5){
for(i=0;i<=8;i++) {
Button btn = new Button(this);
LinearLayout.LayoutParams P = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
P.weight = 1;
btn.setLayoutParams(P);
btn.setText(ActivityTextMen[i]);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Class clas = null;
try{
clas = Class.forName("plkk.developers.com.livfit."+ActivityIdMen[i]);
}catch (ClassNotFoundException c){
c.printStackTrace();
}
if (clas!=null) {
Intent intent = new Intent(view.getContext(), clas);
startActivity(intent);
}
}
});
ll.addView(btn);
}
// the intent always directs me to the class at i=9 (in the above case. I tried solving it by using array of intents but couldn't do that properly.
Remove the weight assigmnent.
Did you declare your activities in te manifest file?
Updated
Try to set a Tag with the index. Then use the value of the tag of your button to get the value.
if(BMI<18.5){
for(i=0;i<=8;i++) {
Button btn = new Button(this);
LinearLayout.LayoutParams P = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
P.weight = 1;
btn.setTag(i);
btn.setLayoutParams(P);
btn.setText(ActivityTextMen[i]);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Class clas = null;
try{
clas = Class.forName("plkk.developers.com.livfit."+ActivityIdMen[Integer.parseInt(""+btn.getTag())]);
}catch (ClassNotFoundException c){
c.printStackTrace();
}
if (clas!=null) {
Intent intent = new Intent(view.getContext(), clas);
startActivity(intent);
}
}
});
ll.addView(btn);
}
This question already has answers here:
Cannot create AlertDialog: AppCompat error
(2 answers)
Closed 6 years ago.
My app crashes when I invoke alert dialogue within a list item. My app lists cars, every item has 2 buttons, each button calls alertdialog for yes/no answer. the app crashes with "You need to use a Theme.AppCompat theme (or descendant) with this activity." everytime I press the button. App code is as follows:
AndroidManifest:
<activity android:name=".SplashScreen" android:theme="#style/generalnotitle">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CarActivity" android:theme="#style/generalnotitle"></activity>
</application>
Style:
<resources>
<style name="generalnotitle" parent="Theme.AppCompat.Light">
<item name="android:windowNoTitle">true</item>
<item name="android:screenOrientation">portrait</item>
</style>
</resources>
CarActivity.java
public class CarActivity extends AppCompatActivity {
String[] car_tag, car_makemodel, car_owner_id;
TypedArray car_pic;
String[] aa_owner_id, aa_owner_name, aa_owner_tlf;
String tlf, name;
List<CarItem> carItems;
ListView mylistview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("list start", "list start" );
getSupportActionBar().hide();
setContentView(R.layout.activity_list);
mylistview = (ListView) findViewById(R.id.LList);
setUpListView();
}
public void setUpListView() {
carItems = new ArrayList<CarItem>();
// gets car info
car_pic = getResources().obtainTypedArray(R.array.a_carpic);
car_tag = getResources().getStringArray(R.array.a_mat);
car_makemodel = getResources().getStringArray(R.array.a_makemodel);
car_owner_id = getResources().getStringArray(R.array.a_owner);
//defines items
for (int i = 0; i < car_tag.length; i++){
CarItem item = new CarItem(car_pic.getResourceId(i, -1), car_tag[i], car_makemodel[i], car_owner_id[i]);
Log.e("CarActivity", car_tag[i] );
carItems.add(item);
}
//gets available owners
aa_owner_id = getResources().getStringArray(R.array.a_id);
aa_owner_name = getResources().getStringArray(R.array.a_name);
aa_owner_tlf = getResources().getStringArray(R.array.a_tlf);
//populates the list
CarAdapter adapter = new CarAdapter(getApplicationContext(), carItems, aa_owner_id , aa_owner_name, aa_owner_tlf);
mylistview.setAdapter(adapter);
}
}
CarAdapter.java
public class CarAdapter extends BaseAdapter {
Context context;
List<CarItem> carItems;
String [] a_owner_id, a_owner_names, a_owner_tlf;
String name, tlf, owner_id;
CarAdapter(Context context, List<CarItem> carItems, String [] a_owner_id, String [] a_owner_names, String [] a_owner_tlf){
this.context = context;
this.carItems = carItems;
this.a_owner_id = a_owner_id;
this.a_owner_names = a_owner_names;
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
convertView = mInflater.inflate(R.layout.row_car,null);
holder = new ViewHolder();
holder.car_pic = (ImageView) convertView.findViewById(R.id.car_pic);
holder.tag = (TextView) convertView.findViewById(R.id.tag);
holder.makemodel = (TextView) convertView.findViewById(R.id.make_model);
holder.owner = (TextView) convertView.findViewById(R.id.owner);
holder.blck = (Button) convertView.findViewById(R.id.block);
holder.mov = (Button) convertView.findViewById(R.id.move);
CarItem row_pos = carItems.get(position);
holder.car_pic.setImageResource(row_pos.getCar_pic());
holder.tag.setText(row_pos.getTag());
holder.makemodel.setText(row_pos.getMakemodel());
owner_id = row_pos.getOwner();
getOwner(owner_id);
holder.owner.setText(name);
holder.blck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBlockClick();
}
});
holder.mov.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("CarAdapter move click", owner_id );
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
private void onBlockClick() {
Log.e("CarActivity", "CLICK block button" );
AlertDialog.Builder alertDlg = new AlertDialog.Builder(context);
alertDlg.setMessage("Do you wish to inform " + name + "?");
alertDlg.setCancelable(false);
alertDlg.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Get owner phone number
getNumber(owner_id);
Log.e("CarActivity YES DIALOG", tlf );
//Toast.makeText(getApplicationContext(),"Afasta-me o teu cangalho...", Toast.LENGTH_SHORT).show();
}
});
alertDlg.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.e("CarActivity", "CLICK NO DIALOG" );
}
});
alertDlg.create().show();
}
}
SOLUTION: when I set adapter, i was sending "getApplicationContext()" but instead i sent "this" and this way i could send the context to the adapter.
CarActivity.java:
before:
CarAdapter adapter = new CarAdapter(getApplicationContext(), carItems, aa_owner_id , aa_owner_name, aa_owner_tlf);
after:
CarAdapter adapter = new CarAdapter(this, carItems, aa_owner_id , aa_owner_name, aa_owner_tlf);
Have you tried using the constructor that takes the dialog's theme as well?
Try this:
AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.generalnotitle);
Then put the rest of the relevant code for the builder.
Note, the theme should point to a dialog theme (Theme.AppCompat.Light.Dialog.Alert). Check out this post for more details.
http://www.materialdoc.com/alerts/
There are two AlertDialog classes: the regular android.app.AlertDialog, and the support/appcompat version android.support.v7.app.AlertDialog. I can't tell which one you are importing, but the latter one requires you to be using an appcompat theme for the given Context, which should not be a problem if you are already using the AppCompat library and your activities extend from AppCompatActivity. (If you aren't using AppCompat, then you should make sure you are using the regular AlertDialog.)
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.
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);
}
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.
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.