Using Scandit BarcodeScanner in 2 fragment of an application - android

I develop an application for scanning barcode. My application is an Android TabView application with 2 tabs. And I want to add barcode scanner in both of them.
My code is following:
Fragment1:
public class WebServiceFragment extends Fragment {
RelativeLayout scanServiceView;
ScanditSDKAutoAdjustingBarcodePicker barcodePicker;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(container == null) {
return null;
}
View v = inflater.inflate(R.layout.webservice_layout, container, false);
scanServiceView = (RelativeLayout)v.findViewById(R.id.scanserviceview);
barcodePicker = new ScanditSDKAutoAdjustingBarcodePicker(getActivity(), "<My Scandit SDK Key 1>", ScanditSDKAutoAdjustingBarcodePicker.CAMERA_FACING_BACK);
barcodePicker.startScanning();
scanServiceView.addView(barcodePicker);
return v;
}
}
Fragment2:
public class CMCSMOFragment extends Fragment {
RelativeLayout scanSMOView;
ScanditSDKAutoAdjustingBarcodePicker barcodePicker;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(container == null) {
return null;
}
View v = inflater.inflate(R.layout.cmcsmo_layout, container, false);
scanSMOView = (RelativeLayout)v.findViewById(R.id.scansmoview);
barcodePicker = new ScanditSDKAutoAdjustingBarcodePicker(getActivity(), "<My Scandit SDK Key 2>", ScanditSDKAutoAdjustingBarcodePicker.CAMERA_FACING_BACK);
barcodePicker.startScanning();
scanSMOView.addView(barcodePicker);
return v;
}
}
But when I build, only Fragment2 display camera view for scanning. Fragment1 display black screen. If I remove Fragment2 code which call Scandit SDK, Fragment1 would work normally.
How should I do with this situation? Image below is the issue screen:
Fragment1:
Fragment2:

As pointed out in the comments you will need to stop the scanning when navigating away from the Fragment.
This is due to the Fragment is not having its view destroyed when simply navigating away. So your reference to the barcodePicker does not get collected as long as you are holding on to that reference.
The camera does not allow multiple sources to connect to it, hence you will have to disconnect from it before attempting to use it somewhere else.

Related

fragment does not go to activity [Android]

It's been less than a week since I learned Android. trying to create a project, but there are some things that don't work.
Problem: Does not move from fragment to activity
Tried: Browse the document on Google. and I used #Override for onclickListener in HomeFragment.java A red line popped up and woke up remove, but still don't know what the error is.
HomeFragment.java
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
private Button btn_today;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
ViewGroup root_view = (ViewGroup)inflater.inflate(R.layout.fragment_home, container, false);
Button btn_today = (Button)root_view.findViewById(R.id.btn_today);
btn_today.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), HomeActivity.class);
startActivity(intent);
}
});
return root;
}
}
used the Navigation Drawer Activity when I first created the project.
what I want to do is click on the button btn_today to go to the activity page (in xml file)
I'm a beginner or I don't know what the problem is. appreciate any help
doing it all day, but I'm not sure.
You should be use root variable for finding button view id.
Button btn_today = (Button)root.findViewById(R.id.btn_today);

How to avoid the layout inflation each time a fragment transaction is committed?

I am using fragments to design my screen.When I navigate back to another fragment (from the back stack), the onCreateView(...) method gets called each time even if the fragment has already been created.How to avoid that the method onCreateView(...) gets called each time and make sure it's called only once (when it's created the first time)?
You can cache your inflated view to the local field if your want. For example:
public class ExampleFragment extends Fragment {
private View fragmentView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
if (fragmentView == null) {
fragmentView = inflater.inflate(R.layout.you_super_view_id, container);
}
return fragmentView;
}
}
But practically, it's ok that pager is reinflating views because it keeps only part of all fragments in memory at the time. So, I think the best idea is to let it work as it should

How to set the right fragment to replace.

I’m doing a navigation drawer in my android app. On my onNavigationDrawerItemSelected function i try on item select to change the current fragment into a new one. The problem is that when I change the current fragment its take the initial status of it. maybe because i'm doing this way
fragmentManager.beginTransaction()
.replace(R.id.cont, new SessionsFragement())
.commit();
I declare a new instance of my fragment and i think it’s normal, its display me the initial status of the thing. On my fragment i've try to set a textView to see if i will say it when i call the fragement from tha navigation drawer but it's not the case.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_spot, container, false);
tata = (TextView) v.findViewById(R.id.tata);
tata.setText("karim");
return inflater.inflate(R.layout.fragment_spot, container, false);
}
So please can anyone help me to fix this.
In the snippet you posted you are inflating again the view. You should return the one you modified
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_spot, container, false);
tata = (TextView) v.findViewById(R.id.tata);
tata.setText("karim");
return v;
}

Save simple variable in Android Fragment

this is my issue, when I create a fragment in android for example let's Say:
GastosSectionFragment gastosFragment = new GastosSectionFragment();
Fragment.gastosFragment.darUserID(userKey);
I send a simple Integer to that activity, no problem. For example lets say I send the number 1. It will be stored in attribute inside that class, because I create the fragment.
Everything will work fine, I receive the number. But when I rotate the phone, the variable "userKEY will be lost, it will converted to cero" I can't save that number when I rotate horizontally my phone, how can I save the data of that variable.
Here I put the code of how I recieve the "userKey" inside the Fragment,
public long darUserID(long userKey) {
// TODO Auto-generated method stub
return user_id = userKey;
}
And the attribute with the onCreate method.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_gastos,
container, false);
idStatico = darUserID(user_id);
And I can't save that little number. this is not a FragmentActivity, this class is just a FRAGMENT. First time fine, rotate screen lost the number that I previously recieve. Fragment doesn't have OnSaveBundleInstances.
Fragment can save user_id in bundle in onSaveInstanceState method and restore after orientation change in onCreateView method. Check this fragment guide, especially in example is shown saving fragment state.
Activity code stays the same (I just changed method name) and your fragment should look like this:
class GastosSectionFragment extends Fragment {
private static final String BUNDLE_USER_ID = "BUNDLE_USER_ID";
private long user_id;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_gastos, container, false);
if (savedInstanceState != null)
user_id = savedInstanceState.getLong(BUNDLE_USER_ID);
// now is your user_id restored after orientation change,
// so you can do some stuff with it
return rootView;
}
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putLong(BUNDLE_USER_ID, user_id);
}
public long setUserID(long userKey) {
this.user_id = userKey;
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
add method override, and you variable saving state
After KOSO answer, this is how I solve the problem, hope it helps someone else.
When I create the Fragment for first time everything work well, send the userKey, everything is fine.
GastosSectionFragment gastosFragment = new GastosSectionFragment();
Fragment.gastosFragment.darUserID(userKey);
Now in order to fix that, a fragment when the phone is rotated, for example to horizontal it will call the override method OnCreateView(). Everytime when you rotate the phone this method will be called.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_section_gastos,
container, false);
//This is not null when we rotate the screen, first we save the Bundle SavedInstance
//After do that we put a integer, in this case the userKey
//then we recover the user key, and everything will work fine.
if (savedInstanceState != null)
{
this.savedInstanceState = savedInstanceState;
savedInstanceState.putInt("userKey", (int) idStatico);
idStatico = savedInstanceState.getInt("userKey");
}
//this will be null for the first time, only the first time this will bse used.
if (savedInstanceState == null)
{
idStatico = darUserID(user_id);
}
//Do the rest of the work
}

how to correctly load webview based on sample of creating swipe view

I am new and learning to create a webview by selecting a tab, based on google's sample at http://developer.android.com/training/implementing-navigation/lateral.html.
I created a class for webview fragment:
public static class WebViewFragment<rootView> extends Fragment {
WebView rootView;
public WebView onCreate(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
inflater.inflate(R.layout.fragment_webview_1, container,false);
rootView.getSettings().setJavaScriptEnabled(true);
rootView.loadUrl("http://www.google.com");
return rootView;
}
}
However, it runs without error but the webview interface never shows up. Appreciate if you can provide any clues.
Above method is actually not called by Android. It is a simple onCreate overloaded method.
Change that method to:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// would be safe to check class
rootView = (WebView)inflater.inflate(R.layout.fragment_webview_1, container,false);
rootView.getSettings().setJavaScriptEnabled(true);
rootView.loadUrl("http://www.google.com");
return rootView;
}

Categories

Resources