ArrayList is null after executing onActivityResult - android

I have Activity > Fragment > RecyclerView .
I am getting data from server in a ArrayList to inflate recyclerView in onCreate() of fragment.
In recyclerView there is Image with which I open camera and click photo.
But when I return after successful image capture, I get my ArrayList null
onCreate in ShipmentFragment :
ArrayList<ShipmentDetails> shipmentList;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
shipmentList = getShipmentLists();
}
in activity's onActivityResult :
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CAMERA_IMAGE_CAPTURE_REQUEST_CODE:
if (resultCode == RESULT_OK) {
ShipmentFragment shipmentFragment = new ShipmentFragment();
shipmentFragment.entryToList();
} else {
Toast.makeText(OrderActivity.this, "Error capturing image", Toast.LENGTH_SHORT).show();
}
break;
}
entryToList() in ShipmentFragment:
public void entryToList() {
// Here I get shipmentList null
int size = shipmentList.size();
}

Related

Can't access onActivityResult zxing for fragment

I'am trying to read the barcode via Zxing in fragment
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_caddie, container, false);
etCodigo = v.findViewById(R.id.etCodigo);
btnLeerCodigo = v.findViewById(R.id.btnLeerCodigo);
btnLeerCodigo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
escanear();
}
});
text = "";
return v;
}
public void escanear() {
IntentIntegrator intent = IntentIntegrator.forSupportFragment(FragmentCaddie.this);
//IntentIntegrator intent = new IntentIntegrator(getActivity());
intent.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
intent.setPrompt("ESCANEAR CODIGO");
intent.setCameraId(0);
intent.setBeepEnabled(false);
intent.setBarcodeImageEnabled(false);
intent.initiateScan();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Toast.makeText(getContext(), "Cancelaste el escaneo", Toast.LENGTH_SHORT).show();
} else {
text = text + " + " + result.getContents().toString() ;
etCodigo.setText(text);
escanear();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
The issue is it doesn't access the onActivityResult
The hosting activity overrides onActivityResult(), but it did not make a call to super.onActivityResult() for unhandled result codes. Apparently, even though the fragment is the one making the startActivityForResult() call, the activity gets the first shot at handling the result. This makes sense when you consider the modularity of fragments. Once I implemented super.onActivityResult() for all unhandled results, the fragment got a shot at handling the result.
Check this out:
onActivityResult is not being called in Fragment
I hope to be useful for u :)
The solution was quiet simple the onActivityResult that i implemented was Overrided from the parent activity
The solution is to call the fragment onActivityResult from the parent activity
private static final int BARECODE_REQUEST = 114910;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == BARECODE_REQUEST) {
super.onActivityResult(requestCode,resultCode,data);
}
}

How to show data in a fragment which comes from an activity

I have created an activity which contains a button, a ViewPager and some fragments. When I click on the button a DialogActivity opens and connects to the web service using AsyncHttpClient and returns some strings which are pictures url, name, etc. to MainActivity. I want MainActivity to show the data in fragment specially the picture url (using Glide) but I can’t do this.
I appreciate it if you help me
DialogActivity:
void parsAndShowBiography(String response) {
try {
Gson gson = new Gson();
TheaudiodbBiography theaudiodbBiography = gson.fromJson(response, TheaudiodbBiography.class);
Intent returnIntent = new Intent(DialogActivity.this, MainActivity.class);
returnIntent.putExtra("artistThumb", theaudiodbBiography.getArtists()
.get(0).getStrArtistThumb());
returnIntent.putExtra("strArtist", theaudiodbBiography.getArtists()
.get(0).getStrArtist());
returnIntent.putExtra("artistFanart1", theaudiodbBiography.getArtists()
.get(0).getStrArtistFanart());
setResult(Activity.RESULT_OK, returnIntent);
finish();
MainActivity :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
???????????????????????
}
And this is the Fragment :
public class BiographyFragment extends Fragment {
static BiographyFragment instance;
public static BiographyFragment getInstance() {
if (instance == null) {
instance = new BiographyFragment();
}
return instance;
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.bigraphy_fragment, container, false);
return view;
}
I put some TextViews and ImageViews into Fragment xml file.
and if it helps this is the UX :
Hi guys i solved the question with this code :
MainActivity :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
View test=BiographyFragment.getInstance().getView();
Glide.with(this).load(data.getStringExtra("artistFanart1")).into(
(ImageView)test.findViewById(R.id.artistFanart1));
}

In Multi Image Selector OnActivityResult() method is not getting called in Fragment

I am trying to make image selector for application.For that i am using multi-image-selector library which is perfeclty works when used in activity ,but here i want to use it in fragment.so in fragment OnActivityResult() method is not getting called.Can anyone help me to solve this?
Here's my code:
MainActivity.java:(My Main Activity)
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_IMAGE = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction().replace(R.id.frame, new Image_Selecter()).commit();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Utilz.printLog("Parentactivity", "onActivityResult");
Log.e("hererererer", "hererererer");
if (requestCode == REQUEST_IMAGE) {
new Image().onActivityResult(requestCode, resultCode, data);
}
}
}
Image:(My Fragment)
public class Image extends Fragment {
private ArrayList<String> mSelectPath;
private static final int REQUEST_IMAGE = 2;
ArrayList<Uri> mMedia = new ArrayList<Uri>();
Uri uri;
ImageView img;
protected static final int REQUEST_STORAGE_READ_ACCESS_PERMISSION = 101;
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_image, container, false);
img = (ImageView) view.findViewById(R.id.img);
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pickImage();
}
});
return view;
}
public void pickImage() {
MultiImageSelector selector = new MultiImageSelector(getActivity());
selector.showCamera(true);
selector.multi();
selector.count(1);
selector.origin(mSelectPath);
selector.start(getActivity(), REQUEST_IMAGE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE) {
if (resultCode == getActivity().RESULT_OK) {
mSelectPath = data.getStringArrayListExtra(MultiImageSelector.EXTRA_RESULT);
mMedia.clear();
for (String p : mSelectPath) {
Uri uri = Uri.parse(p);
mMedia.add(uri);
}
uri = mMedia.get(0);
Log.e("uri", " " + uri);
if (!uri.toString().contains("content://")) {
uri = Uri.fromFile(new File(uri.toString()));
Log.e("in if", " uri = " + uri);
}
try {
Glide.with(this)
.load(uri)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(img);
} catch (Exception e) {
Log.e("Exceptionn", " " + e);
}
}
}
}
Try this in your activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("Parentactivity", "onActivityResult");
if (requestCode == REQUEST_IMAGE) { //use request code as REQUEST_IMAGE while starting intent for camera
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frame);
fragment.onActivityResult(requestCode, resultCode, data);//calling fragments onActivityResult here
}
}
IIRC onActivityResult doesn't get called on a Fragment, it gets called on your parent Activity. You would need to catch this in the Activity and forward the result on to the fragment.
There are two methods which looks same but behaves little bit different.
If method is called by from activity, its not going to give callback to fragment unless called from fragment.
Activity.startActivityForResult()
Fragment.startActivityForResult()
Check out the library if there is way to calling it from fragment.
I dont know the library, just imaging if you can pass fragment to
MultiImageSelector selector = new MultiImageSelector(getActivity()); or selector.start(getActivity(), REQUEST_IMAGE);
Good luck
Emre
you have to call it with intent
public void pickImage() {
Intent intent = new Intent(getContext(), MultiImageSelectorActivity.class);
intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 1);
intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_MULTI);
intent.putStringArrayListExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, mSelectPath);
startActivityForResult(intent, REQUEST_IMAGE);
}
if you were doing getActivity().startActivityForResult(intent, REQUEST_IMAGE);
the on OnActivityResult() will happen on activity instead of fragment. The way you are using it is like that, so you need to use the intent way.
also that maybe will make your code to work
public void pickImage() {
MultiImageSelector selector = new MultiImageSelector(getContext());
selector.showCamera(true);
selector.multi();
selector.count(1);
selector.origin(mSelectPath);
selector.start(getContext(), REQUEST_IMAGE);
}

Android ArrayAdapter for ListView method .notifyDataSetChanged() issue

I am trying to update ListView after another activity finish()'es with result. Initialization (works correctly):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseHelper = new DatabaseHelper(this);
objects = databaseHelper.selectObjects();
objectsListView = (ListView) findViewById(R.id.LIST_VIEW_OBJECTS);
objectArrayAdapter = new ArrayAdapter<Object>(this, android.R.layout.simple_list_item_1, objects);
objectsListView.setAdapter(objectArrayAdapter);
}
Problem occurs only when Im trying update ListView:
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if(requestCode == SECOND_ACTIVITY_REQUEST) {
if (resultCode == RESULT_OK) {
objects = databaseHelper.selectObjects();
objectArrayAdapter.notifyDataSetChanged();
}
if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.TOAST_ERROR_RESULT_CANCELED), Toast.LENGTH_LONG).show();
}
}
}
But this code works just fine:
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if(requestCode == SECOND_ACTIVITY_REQUEST) {
if (resultCode == RESULT_OK) {
objects.add(newObject);
objectArrayAdapter.notifyDataSetChanged();
}
if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.TOAST_ERROR_RESULT_CANCELED), Toast.LENGTH_LONG).show();
}
}
}
This code also works fine:
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if(requestCode == SECOND_ACTIVITY_REQUEST) {
if (resultCode == RESULT_OK) {
objects = databaseHelper.selectObjects();
objectArrayAdapter = new ArrayAdapter<Object>(this, android.R.layout.simple_list_item_1, objects);
objectsListView.setAdapter(objectArrayAdapter);
}
if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.TOAST_ERROR_RESULT_CANCELED), Toast.LENGTH_LONG).show();
}
}
}
My question is: why first method doesn't work as expected? Second and third seems to be a work round isn't it?
In your first example, you are giving the information to questionnaires variable, which I don't see being related to your listview. onNotifyDataSetChanged() has the same data as before from what I can tell.
The second one works because you are directly changing the objects variable which is connected to your adapter.
EDIT:
"For an ArrayAdapter, notifyDataSetChanged only works if you use the add(), insert(), remove(), and clear() on the Adapter."
notifyDataSetChanged example

Xamarin.Android OnActivityResult not being called inside a Fragment

It appears as if OnActivityResult does not get called after accepting the picture taken from the camera.
Am I calling StartActivityForResult() wrong?, or is there something I am missing.
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Android.OS.Bundle savedInstanceState)
{
var m_View = inflater.Inflate (Resource.Layout.Feed, null);
btnCamera = m_View.FindViewById<Button> (Resource.Id.btnCamera);
btnGallery = m_View.FindViewById<Button> (Resource.Id.btnGallery);
ivPicture = m_View.FindViewById<ImageView> (Resource.Id.imageView1);
btnCamera.Click += (sender, e) => {
//launch gallery
//allow photo editing/saving
var mediaPicker = new MediaPicker (Activity);
if (!mediaPicker.IsCameraAvailable){
// Console.WriteLine ("No Camera!");
}else {
Intent intent = new Intent(MediaStore.ActionImageCapture);
StartActivityForResult(intent, 0);
}
};
btnGallery.Click += (sender, e) => {
//launch gallery
//allow photo editing/saving
var imageIntent = new Intent ();
imageIntent.SetType ("image/*");
imageIntent.SetAction (Intent.ActionGetContent);
StartActivityForResult (
Intent.CreateChooser (imageIntent, "Select photo"), 0);
};
return m_View;
}
protected virtual void OnActivityResult (int requestCode, Result resultCode, Intent data)
{
System.Console.WriteLine ("WOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
Uri contentUri = data.Data;
ivPicture.SetImageURI (data.Data);
}
Hmm.. I created a sample and it worked just fine for me. The only difference I see is that it looks like your override is not correct. It should be public override void OnActivityResult()
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var rootView = inflater.Inflate(Resource.Layout.MainFragment, container, false);
var button = rootView.FindViewById<Button>(Resource.Id.button_camera);
button.Click += (sender, args) =>
{
var intent = new Intent(MediaStore.ActionImageCapture);
StartActivityForResult(intent, 0);
};
return rootView;
}
public override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
Log.Debug("TestFragment", "Got result");
// do what you want with the result here
}
Try changing the data type of resultCode to int, instead of Result and making the overridden function public.
e.g.
public override void OnActivityResult(int requestCode, int resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
}
For the people that use Android.Support.V4.App Fragment. You can override the OnActivityResult method in your Fragment but it will never be called if you use this in combination with a FragmentActivity!
It will be the OnActivityResult of the FragmentActivity that will be called. This can be implemented like following:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
}
Check for request code is same as provided for startActivityForResult(intent, 0) here 0 is request code.. eg if you put 999 here insted of 0 ..then in onActivity result method check for requestcode == 999. it is used to check if the intent is the same as asked for and nothing other than that..
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == RESULT_OK) { // ADD THIS
// Get Data and do something with it
}
}

Categories

Resources