I am trying to get a callback from an activity that I am starting with an intent in my fragment.
I thought I could do this with onActivityResult but it doesn't seem to get called when I finish(); on the activity. Is this possible in a fragment?
Fragment.java
#Nullable
#Override
public View onCreateView(final LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
getActivity().setTitle("Waiver");
myView = inflater.inflate(R.layout.waiver_layout, container, false);
signBtn = myView.findViewById(R.id.signBtn);
signBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), SignatureActivity.class);
intent.putExtra("signatureAbleId", device.id);
intent.putExtra("signatureAbleType", "App\\Models\\Device");
startActivity(intent);
}
});
return myView;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
Log.v("Activity", "The activity has finished");
if(resultCode == 200){
saveWaiver();
}
}
private void saveWaiver(){
Log.v("Save Waiver", "Saving waiver for you.");
}
SignatureActivity.java
public class SignatureActivity extends AppCompatActivity {
private Button btnClear;
private Button btnSave;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_signature);
Bundle bundle = getIntent().getExtras();
if(bundle != null){
signatureAbleId = bundle.getInt("signatureAbleId");
signatureAbleType = bundle.getString("signatureAbleType");
}
btnClear = findViewById(R.id.btnClear);
btnSave = findViewById(R.id.btnSave);
btnClear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mSignaturePad.clear();
}
});
btnSave.setOnClickListener(new View.OnClickListener() {
setResult(200);
finish();
});
}
}
Use this. It will help you
Intent intent = new Intent(getContext(), SignatureActivity.class);
Instead of
Intent intent = new Intent(getActivity(), SignatureActivity.class);
And Yes need to change :
startActivity to startActivityForResult
Use startActivityForResult instead of startActivity
startActivityForResult(intent, 11);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 11){
if(resultCode == 200){
saveWaiver();
}
}else{
super.onActivityResult(requestCode, resultCode, data)
}
}
Related
I need a camera function in my CameraFragment.java file. I already have the code for the camera and it is working in a empty application (when I put it in my MainActivity), but I don't know where to put the code in my CameraFragment.java.
I am really a beginner with Android Studio, but I couldn't find the answer on the internet. Also new on Stack Overflow.
CameraFragment.java
public class CameraFragment extends Fragment{
public static final String EXTRA_INFO = "default";
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_camera, container, false);
}
}
AND I NEED THIS CODE IN MY CameraFragment FILE:
public class MainActivity extends AppCompatActivity {
private Button btnCapture;
private ImageView imgCapture;
private static final int Image_Capture_Code = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_camera);
btnCapture =(Button)findViewById(R.id.btnTakePicture);
imgCapture = (ImageView) findViewById(R.id.capturedImage);
btnCapture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cInt = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cInt,Image_Capture_Code);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
if (requestCode == Image_Capture_Code) {
if (resultCode == RESULT_OK) {
Bitmap bp = (Bitmap) data.getExtras().get("data");
imgCapture.setImageBitmap(bp);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
}
}
}
}
Let me know how this works for you. Comment if you need more help setting this up.
public class CameraFragment extends Fragment {
public static final String EXTRA_INFO = "default";
private Button btnCapture;
private ImageView imgCapture;
private static final int Image_Capture_Code = 1;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_camera, container, false);
btnCapture =(Button) view.findViewById(R.id.btnTakePicture);
imgCapture = (ImageView) view.findViewById(R.id.capturedImage);
btnCapture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cInt = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cInt,Image_Capture_Code);
}
});
return view;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Image_Capture_Code) {
if (resultCode == RESULT_OK) {
Bitmap bp = (Bitmap) data.getExtras().get("data");
imgCapture.setImageBitmap(bp);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getActivity(), "Cancelled", Toast.LENGTH_LONG).show();
}
}
}}
In fragment use same as activity but make method public
try this code
public class ChatFragment extends Fragment {
private RecyclerView chatRecylerview;
View view;
ChatUserlistAdapter userlistAdapter;
LinearLayoutManager manager;
ArrayList<HashMap<String, String>> userDetail = new ArrayList<>();
HashMap<String, String> data;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_chat, container, false);
btnCapture =(Button)view.findViewById(R.id.btnTakePicture);
imgCapture = (ImageView)view.findViewById(R.id.capturedImage);
btnCapture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cInt = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cInt,Image_Capture_Code);
}
});
return view;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Image_Capture_Code) {
if (resultCode == RESULT_OK) {
Bitmap bp = (Bitmap) data.getExtras().get("data");
imgCapture.setImageBitmap(bp);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
}
}
}
}
I have this button which should play the video on another activity or to be specific in Main2Activity. The problem is that the video wouldn't play when I click the button. Everything is working fine, the only problem I have is when I click that said button.
MainActivity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final Uri videoUri = data.getData();
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
resultvideo.setVideoURI(videoUri);
mediacontroller.setAnchorView(resultvideo);
resultvideo.pause();
}
buttonPlay = (Button) findViewById(R.id.buttonPlay);
{
buttonPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediacontroller.show();
mediacontroller.setAnchorView(resultvideo);
resultvideo.start();
}
});
}
buttonFullScreen = (Button) findViewById(R.id.buttonFullScreen);
{
buttonFullScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("VIDEO_URI", videoUri.toString());
startActivity(intent);
}
});
}
}
Main2Activity:
public class Main2Activity extends Activity {
Button buttonPlay;
Button buttonFullScreen;
static final int REQUEST_VIDEO_CAPTURE = 1;
VideoView resultvideo;
MediaController mediacontroller;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String uri = getIntent().getStringExtra("VIDEO_URI");
Uri videoUri = Uri.parse(uri);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main2);
resultvideo = (VideoView)findViewById(R.id.videoView2);
resultvideo.start();
}
}
you will need to add
resultvideo.setVideoURI(videoUri);
after you initialed the resultvideo.
Everything works fine without the Main2Activity but what I want to do is to play the video on Main2Activity when I click the fullscreen button. Everything works well on MainActivity but when I click the fullscreen button it crashes. Not sure why. I'm new to Android development, any help would be appreciated.
MainActivity:
public class MainActivity extends AppCompatActivity {
Button buttonPlay;
Button buttonFullScreen;
static final int REQUEST_VIDEO_CAPTURE = 1;
VideoView resultvideo;
MediaController mediacontroller;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setContentView(R.layout.activity_main);
setContentView(R.layout.activity_main);
resultvideo = (VideoView)findViewById(R.id.videoView);
mediacontroller = new MediaController(MainActivity.this);
mediacontroller.setAnchorView(resultvideo);
resultvideo.setMediaController(mediacontroller);
Button click = (Button)findViewById(R.id.buttonRecord);
resultvideo = (VideoView)findViewById(R.id.videoView);
}
public void dispatchTakeVideoIntent(View v) {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
Uri videoUri = data.getData();
Log.i("test","111111111111111" + videoUri.toString());
resultvideo.setVideoURI(videoUri);
mediacontroller.setAnchorView(resultvideo);
resultvideo.pause();
}
buttonPlay = (Button) findViewById(R.id.buttonPlay);
{
buttonPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediacontroller.show();
mediacontroller.setAnchorView(resultvideo);
resultvideo.start();
Log.i("test","111111111111111");
}
});
}
buttonFullScreen = (Button) findViewById(R.id.buttonFullScreen);
{
buttonFullScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Main2Activity.class));
}
});
}
}
}
Main2Activity:
public class Main2Activity extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main2);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
Uri videoUri = data.getData();
Log.i("test", "111111111111111" + videoUri.toString());
resultvideo.setVideoURI(videoUri);
mediacontroller.setAnchorView(resultvideo);
resultvideo.pause();
}
buttonFullScreen = (Button) findViewById(R.id.buttonFullScreen);
{
buttonFullScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediacontroller.show();
mediacontroller.setAnchorView(resultvideo);
resultvideo.start();
Log.i("test","111111111111111");
}
});
}
}
}
resultvideo is declared in MainActivity, it is not available in Main2Activity
mediacontroller and resultvideo are declared in MainActivity where they are in Main2Activity? You must have them in layout for Main2Activity also, as you have them in MainActivity layout and finding them by ids in Main2Activity is also necessary.
I have a button in Claims.java. When button is pressed, it will show Alert Dialog Window with radio buttons.If the radio button is checked, it will goes to specific activity. In the activity, it has an editText and a save button. I want the value on the editText display on the button(Claims.java) when the save button in the activity is clicked.
Claims.java >> AlertDialog Window in Claims.java >> AlertDialogRadio.java
I use startActivityForResult() to receive a result back from AlertRadioDialog.java. But the problem now is it will display AlertDialogRadio which is not what I want and the text does not display on the textView. How can I do to achieve this?
Claims.java
public class Claims extends Fragment {
private TextView c;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View claims = inflater.inflate(R.layout.claims, container, false);
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
AlertDialogRadio();
}
};
Button button1 = (Button) claims.findViewById(R.id.button10);
Button button = (Button) claims.findViewById(R.id.button8);
button1.setOnClickListener(listener);
c=(TextView)claims.findViewById(R.id.textView49);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(getActivity().getApplicationContext(), CameraMain.class);
startActivity(intent);
}
});
return claims;
}
public void AlertDialogRadio() {
final CharSequence[] ClaimsModel = {"Project", "Petrol", "Car Maintenance"
, "Medical", "Other"};
AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
alt_bld.setTitle("Select a Claims");
alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(getActivity().getApplicationContext(), Project1.class);
startActivity(intent);
} else if (item == 1) {
Intent intent = new Intent(getActivity().getApplicationContext(), Petrol.class);
startActivity(intent);
} else if (item == 2) {
Intent intent = new Intent(getActivity().getApplicationContext(), CarMainten.class);
startActivity(intent);
} else if (item == 3) {
Intent intent = new Intent(getActivity().getApplicationContext(), Medical.class);
startActivity(intent);
} else if (item == 4) {
Intent intent = new Intent(getActivity().getApplicationContext(), Other.class);
startActivity(intent);
}
}
});
AlertDialog alert = alt_bld.create();
alert.show();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("text");
c.setText(result);
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//onActivityResult
}
Assume the user choose Project.
Project1.java
public class Project1 extends AppCompatActivity {
private static String text;
private static EditText txt;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.project);
txt= (EditText)findViewById(R.id.editText36);
Button b=(Button)findViewById(R.id.button17);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent returnIntent = new Intent();
text = txt.getText().toString();
returnIntent.putExtra("text", text);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
}
}
First of all, you're not using startActivityForResult at all.
Here's how you should proceed:
Claims.java
public static final int PROJECT_REQUEST_CODE = 1;
public static final int CAMERA_REQUEST_CODE = 2;
public static .....
if (item == 0) {
Intent intent = new Intent(getActivity().getApplicationContext(), Project1.class);
startActivityForResult(intent, PROJECT_REQUEST_CODE);
}
else if .....
And in OnActivityResult :
if (requestCode == PROJECT_REQUEST_CODE) {
...
}
else if(requestCode == CAMERA_REQUEST_CODE) {
...
}
else if ...
I have a showAddForm button like the imej below in Claims.java.
When the + button is pressed, it will show Alert Dialog Window with radio buttons. When the radio button is checked, it will goes to specific activity. In the activity, it has an editText and a save button. I want the value on the editText display on the showAddForm button when the save button in the activity is clicked. How can I do to achieve this?
Claims.java
public class Claims extends Fragment {
private TextView c;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View claims = inflater.inflate(R.layout.claims, container, false);
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
AlertDialogRadio();
}
};
Button button1 = (Button) claims.findViewById(R.id.button10);
Button button = (Button) claims.findViewById(R.id.button8);
button1.setOnClickListener(listener);
c=(TextView)claims.findViewById(R.id.textView49);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(getActivity().getApplicationContext(), CameraMain.class);
startActivity(intent);
}
});
return claims;
}
public void AlertDialogRadio() {
final CharSequence[] ClaimsModel = {"Project", "Petrol", "Car Maintenance"
, "Medical", "Other"};
AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
alt_bld.setTitle("Select a Claims");
alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(getActivity().getApplicationContext(), Project1.class);
startActivity(intent);
} else if (item == 1) {
Intent intent = new Intent(getActivity().getApplicationContext(), Petrol.class);
startActivity(intent);
} else if (item == 2) {
Intent intent = new Intent(getActivity().getApplicationContext(), CarMainten.class);
startActivity(intent);
} else if (item == 3) {
Intent intent = new Intent(getActivity().getApplicationContext(), Medical.class);
startActivity(intent);
} else if (item == 4) {
Intent intent = new Intent(getActivity().getApplicationContext(), Other.class);
startActivity(intent);
}
}
});
AlertDialog alert = alt_bld.create();
alert.show();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("text");
c.setText(result);
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//onActivityResult
}
Assume the user choose Project.
Project1.java
public class Project1 extends AppCompatActivity {
private static String text;
private static EditText txt;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.project);
txt= (EditText)findViewById(R.id.editText36);
Button b=(Button)findViewById(R.id.button17);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent returnIntent = new Intent();
text = txt.getText().toString();
returnIntent.putExtra("text", text);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
}
}
Now when I click save button in Project1.java, it return to the AlertDialogRadio which is not I want and the text still not displaying on the textView!!! ANYONE CAN HELP?????
You can use startActivityForResult() and override onActivityResult() in your activity. You can use setResult() in the second activity to pass whatever you want to your caller activity.
Example here and here
edit:
If you want it to use from a Fragment, you have to write this in your Activity that is holding the Fragment:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
This will pass the result to your fragment, so you can handle the result inside your fragment.
And as I already mentioned, you have to use startActivityForResult() instead of startActivity()
as #keybee said you should use startActivityForResult() to start the project.class from your dialog.
In the Project1.Class when you click save use setResult() to send the value back to the claims.class(Dont do startActivity() again. just use finish() ).
and in your Claims.class use onActivityResult(int requestCode, int resultCode, Intent data) to recieve the value sent from the project1.class.
you can do a setText() on showAddForm button in the onActivityResult(int requestCode, int resultCode, Intent data) function