#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = FragmentAlarmBinding.inflate(getLayoutInflater());
return binding.getRoot();
createNotificationChannel(); //problem is here. The name of the problem "Unreachable statement"
binding.cancelAlarmBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cancelAlarm();
}
});
}
So you have to familiarize yourself with what a return keyword does in a method. You can go through this article.
In Java, the return keyword returns a value from a method. The method will return the value immediately when the keyword is encountered. This means that the method will not execute any more statements beyond the return keyword, and any local variables created in the method will be discarded.
So back to your case, you have the following code:
#Override public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);
binding = FragmentAlarmBinding.inflate(getLayoutInflater());
return binding.getRoot();
createNotificationChannel(); //problem is here. The name of the problem "Unreachable statement"
binding.cancelAlarmBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cancelAlarm();
}
});
}
You are having return binding.getRoot(); in the second line of your method. And that is already an ending point of your method. All the remaining lines you have within the method will not be executed. And hence that's why Unreachable statement will be prompted.
So the correct code for onCreateView() function should look like this:
#Override public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);
binding = FragmentAlarmBinding.inflate(getLayoutInflater());
return binding.getRoot();
}
Then you may wonder how to deal with the remaining lines of your method? So for a Fragment, after the View is being loaded with onCreateView(), onViewCreated() will be called and you can do your next action there. So you should implement another override method onViewCreated():
#Override public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);
binding = FragmentAlarmBinding.inflate(getLayoutInflater());
return binding.getRoot();
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
createNotificationChannel();
binding.cancelAlarmBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cancelAlarm();
}
});
}
Related
I am trying to make a scientific calculator inside a fragment. Actually following a YT video and that guy is doing all that in his mainactivity not the fragment. I used this code in xml of my fragment:
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_scientific_calculator, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
previousCalculation = view.findViewById(R.id.previousCalculationView);
display = view.findViewById(R.id.displayEditText);
}
private void updateText(String strTOAdd) {
display.setText(strTOAdd);
}
public void zeroBTNPush(View view) {
updateText("0");
}
All I know is that to use findViewById in a fragment, I have to create a onViewCreated void. So after doing all this I should get the updateText function in fragment.java>design>attributes>onClick but I get nothing there
enter image description here
I'm new to this. Please help
You can just set OnClickListener in your fragment code like this
someBtn.setOnClickListener(new OnClickListener(){
// call the method you want to be excecuted when the buttun is being pressed
})
also you can specify it in the design, but for example I would prefer to work with the XML code directly - it's much more understantable for me
To use findViewById I think all you need is a View object, which is returned by onCreateView, so you could just do:
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_scientific_calculator, container, false);
Button btn = view.findViewById(R.id.yourButtonId);
btn.setOnClickListener((View v) -> {
// do something here
});
}
You can do this in onViewCreated with the view that is passed in by getting the button and setting a click listener on it like this:
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//...
Button myButton = view.findViewById(R.id.myButton);
myButton.setOnClickListener((View v) -> {
// do button stuff
});
}
I am using androidx jetpack navigation component in my application and I am confused on how to call a method in previous fragment from the current fragment.
There is currently no resource online on how to do this.
My implementation is below...
FRAGMENT A
This is the code sample of the first fragment.
public class FragmentOne extends Fragment{
private String holder;
private NavController navController;
private Button btn_next;
private void findViews(View view){
btn_next = view.findViewById(R.id.btn_next);
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
return view;
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
navController = Navigation.findNavController(view);
findViews(view);
holder = "VALUE IN HOLDER VARIABLE";
btn_next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
navController.navigate(R.id.action_fundone_to_fragmenttwo);
}
});
}
public void executeInFragmentTwo(){
System.out.println(holder);
Toast.makeText(getContext(), holder, Toast.LENGTH_LONG).show();
}
}
FRAGMENT TWO
This is the code sample of the second fragment
public class FragmentTwo extends Fragment{
private NavController navController;
private Button btn_exec_fragment_one_method;
private void findViews(View view){
btn_exec_fragment_one_method= view.findViewById(R.id.btn_exec_fragment_one_method);
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
return view;
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
navController = Navigation.findNavController(view);
findViews(view);
btn_exec_fragment_one_method.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//HOW DO I CALL FRAGMENT ONE METHOD FROM HERE
//The method with name "executeInFragmentTwo"
}
});
}
}
Please any help on this would really be appreciated.
Your first fragment will not be in a resumed state when the user is onto the second fragment.
If you need a function to be executed from another screen/fragment then you probably need to move this function to a ViewModel that is either tied to the lifecycle of the graph that these screens/fragments are part of or to the host activity lifecycle so that you get the same instance in both screens/fragments.
To inject a model that lives as long as your graph you use the following syntax:
private val model: MyViewModel by navGraphViewModels(R.id.myGraph) { ViewModelFactory() }
So I haven't seen any question like this and I have a problem I don't know how to start the other class from the fragment. I have been trying all day and I still can't find the answer. First of all how do I start my imageadapter class and how the hell can I write additional methods into the class and then execute them in the fragment class?
public class ImageFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_image, container, false);
GridView gridView = (GridView)getView().findViewById(R.id.gridView);
gridView.setAdapter(new imageadapter(getContext()));
}
}
but why does this then not work ?
public class ImageFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_image, container, false);
}
#Override
public void onViewCreated(#NonNull final View view, #Nullable Bundle savedInstanceState) {}
GridView gridView = (GridView)getView().findViewById(R.id.gridView);
gridView.setAdapter(new imageadapter(this));
}
I still get errors by the words this and the setAdapter.
All you need to do is add an onViewCreated method where you can do call your methods and other stuff. Here's my code.
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_upcoming, container, false);
}
#Override
public void onViewCreated(#NonNull final View view, #Nullable Bundle savedInstanceState) {
//you can call your methods and find your views in here
setHasOptionsMenu(true);
final FloatingActionButton fab = view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), CreateEditAppointmentActivity.class);
startActivityForResult(intent, RC_ADD_CLIENT);
}
});
coordinatorLayout = view.findViewById(R.id.upcomingLayout);
}
public class select_fragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_select, null);
}
private void button_parking(){
Intent myIntent = new Intent(f, parking.class);
startActivity(myIntent);
}
}
try this...
public class select_fragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
Button your_button = (Button) getActivity.findViewById(R.id.your_id_button)
your_button.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
button_parking();
}
});
return inflater.inflate(R.layout.fragment_select, null);
}
private void button_parking(){
Intent myIntent = new Intent(getActivity(), parking.class);
startActivity(myIntent);
}
}
You haven't bind the view to your fragment so the click button can't work. You need to bind the view with findViewById(). Usually you need to do the binding by overriding onViewCreated() something like this:
public class select_fragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_select, null);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// bind the view here.
Button button = findViewById(R.id.your_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//call button method here
button_parking();
}
});
}
private void button_parking() {
Intent myIntent = new Intent(f, parking.class);
startActivity(myIntent);
}
}
public class InputFragment extends Fragment {
public ContractMVP.start start;
void setPresenter(ContractMVP.start start){
this.start=start;
}
#BindView(R.id.Start_btn)Button btn;
#OnClick(R.id.Start_btn) void setBtn() {
start.start();
}
public static InputFragment newInstance() {
return new InputFragment();
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_input, container, false);
ButterKnife.bind(this,v);
return v;
}
I get NullpointException when ever I try to click the Button. When I debug it says start=null,
but when I use traditional findViewByid it works fine
Did you add both dependences
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor'com.jakewharton:butterknife-compiler:8.8.1'
Also check XML view Id and R.id.(yourId)