First of all sorry if this post may seem a duplicate but I am very new to Android programming and posting this question only when I am still not able to get a satisfactory answer for use of getActivity.
Theoretically I understand the use of getActivity() from the several posts here but I am confused how it is working in my code.
I have a class MainActivity from which I am creating a dialog onclick of a checkbox. I have another class TaxDialog where the dialog is implemented. On click of Yes/No buttons I am calling methods define in MainActivity class. Below are the codes:
MainActivty
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements View.OnClickListener, View.OnFocusChangeListener {
// onCheck of checkbox showNoticeDialog is called
public void showNoticeDialog() {
// Create an instance of the dialog fragment and show it
Log.i("MyActivity", "Inside showNoticeDialog");
DialogFragment dialog = new TaxDialog();
Bundle args = new Bundle();
args.putString("title", "Test");
args.putString("message", "Test Message");
dialog.setArguments(args);
dialog.show(getSupportFragmentManager(), "dialog");
}
public void doPositiveClick(){
Log.i("MyActivity", "Inside +ve");
}
public void doNegativeClick(){
Log.i("MyActivity", "Inside -ve");
}
}
TaxDialog
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
public class TaxDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle args = getArguments();
String title = args.getString("title");
String message = args.getString("message");
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, null);
Log.i("MyActivity", "Expected fault area.");
((MainActivity) getActivity()).doPositiveClick();
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((MainActivity) getActivity()).doNegativeClick();
}
});
builder.create();
return builder.create();
}
}
Here I want to understand how below line works
((MainActivity) getActivity()).doPositiveClick();
and also please make me aware of other ways of doing the same thing (something like MainActivity.this.getActivity() or something else).
Thanks a lot.
EDIT
Thanks everyone. Probably I framed the question incorrectly. My only doubt was how getActivity() returns the Activity reference. Now I understand.