Reputation: 59
I have used the standard fragment that comes with android studio as i thought it would simplify the things for me as i am still learning java. but with too much code inside already i got lost and don't know how things work anymore. Activities i seem to understand the basics but there is a big difference between activities and fragments following some research
I am trying to apply the same principles i learned for activities on fragments but it doesnt seem to be possible. it says that i'm suppose to override the oncreateview with an inflater but i don't seem to understand the concept behind, why can't i use simple setcontentview and retrieve the content from the imageview and textedit? is there a workaround or is it better for me to avoid fragments ?
package com.venomdev.safestorage;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link EncryptFiles.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link EncryptFiles#newInstance} factory method to
* create an instance of this fragment.
*
*/
public class EncryptFiles extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
ImageView UploadFile;
Button bUpload;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment EncryptFiles.
*/
// TODO: Rename and change types and number of parameters
public static EncryptFiles newInstance(String param1, String param2) {
EncryptFiles fragment = new EncryptFiles();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public EncryptFiles() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.UploadFile);
UploadFile = (ImageView) findViewById(R.id.UploadFile);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_encrypt_files, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) activity;
} else {
throw new RuntimeException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
Upvotes: 0
Views: 5120
Reputation: 1830
Yes Like Niko Yuwono, mentioned in his answer. Your onCreateView have to be like this. So that the Ui can be inflated with the java code, where you can handle UI clicks and Interactions from the user with design on the screen.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
final View rootview = inflater.inflate(R.layout.fragment_encrypt_files, container, false);
return rootview;
}
you have to remove Oncreate() method in your code, as this is not an Activity.
Upvotes: 2
Reputation: 11122
Because Fragment is located inside an Activity, and can not be used apart from one. You can't use Fragment as full replacement of Activity but you can use Fragment as a part of your Activity that can be reused in another activity too.
You need to call onCreateView()
because the system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.
For further understanding of Fragment you should read the official documentation
Upvotes: 3