I have two activities.
Home activity contain a list view with two buttons named checkIn and directions.when checkIn button is clicked it does some operation (say A).
When direction button is clicked it launches directions activity.So in directions activity if some condition satisfies there comes an alertbox asking for whether to check In or not.If yes is clicked,I want to do operation A on the checkin button but without destroying directions activity.ie,I want to control the button OnclickListener with the status of an alertbox in other activity without losing the state of present activity.
this is the portion of the code in getView of Listadapter used by Listview of Home activity.btnChild1 is the CheckIn button.
btnChild1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (btnChild1.getText().toString().equals("Check In")) {
btnChild1.setText("Cancel");
taskSubList.get(position).setCheckIn(1);
startTime = System.currentTimeMillis();
} else {
btnChild1.setText("Check In");
taskSubList.get(position).setCheckIn(0);
long difference =taskSubList.get(position).getTimeSpent() +System.currentTimeMillis() - startTime;
taskSubList.get(position).setTimeSpent(difference);
String values[]={Integer.toString(taskSubList.get(position).getId()), Integer.toString((int) difference)};
String updateTime=Helper.getfromUrl(updateTimeUrl,values);
if (!updateTime.equals("success"))
{
Toast.makeText(context, "Not updated", Toast.LENGTH_SHORT).show();
}
Intent reasonIn = new Intent(context.getApplicationContext(), Reason.class);
context.startActivity(reasonIn);
}
}
});
And in Directions.java
if((int)distance/1000 <= 30 && checkInStatus == 0)
{
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
alertbox.setMessage("Do you want to Check In?");
alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// I have to do the above function
}
});
alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
}
});
alertbox.show();
}
What you could do is write a checkIn function and tie both of these buttons to it, it all depends on what you need to do. This way you create a utility class that handles the checkIn etc. but tie both the button in activities.Home and activities.Directions to operation A in the utility class.
"without destroying directions activity" - Just dont call finish() method and the activity will not be destroyed but will be hold only "onPause".
In your situation you need to be more specific what the checkln stuff does. if it somehow changed the UI of the first activity or not for instance.
If the checkln just display something (image, text, data from web service) just call
and then when the time for displaying infromation is fullfied just finish the newly created activity and call
and you are back to your second activity displaying the same what it was displaying before the alertbox was shown.
What you have to do is:
Import the needed library:
On your secondary activity, you define:
Then, on your main activity, you say:
Finally, when you want to call that button of the main Activity from the secondary one: