Can I use OnCreateView with the Activity or is only used with fragments?

2.6k Views Asked by At

If OnCreateView is only for fragments, what would be for Activity?
I tried OnCreate() and I cannot make it work

In the first Override I have problems, and in the last Overide it also give me an error for the OncreateView.
I have read about OnCreate() and OnCreateView(), but I cannot find the answer.

private VrPanoramaView panoWidgetView;
private ImageLoaderTask backgroundImageLoaderTask;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v =  inflater.inflate(R.layout.activity_pruebafoto, container,false);
    panoWidgetView = v.findViewById(R.id.pano_view);
    return v;
}

@Override
public void onPause() {
    panoWidgetView.pauseRendering();
    super.onPause();
}

@Override
public void onResume() {
    panoWidgetView.resumeRendering();
    super.onResume();
}

@Override
public void onDestroy() {
    // Destroy the widget and free memory.
    panoWidgetView.shutdown();
    super.onDestroy();
}

private synchronized void loadPanoImage() {
    ImageLoaderTask task = backgroundImageLoaderTask;
    if (task != null && !task.isCancelled()) {
        // Cancel any task from a previous loading.
        task.cancel(true);
    }

    // pass in the name of the image to load from assets.
    VrPanoramaView.Options viewOptions = new VrPanoramaView.Options();
    viewOptions.inputType = VrPanoramaView.Options.TYPE_STEREO_OVER_UNDER;

    // use the name of the image in the assets/ directory.
    String panoImageName = "@drawable/iglesiavr.jpg";

    // create the task passing the widget view and call execute to start.
    task = new ImageLoaderTask(panoWidgetView, viewOptions, panoImageName);
    task.execute(this.getAssets());
    backgroundImageLoaderTask = task;
}

@Override
public void onCreateView(@Nullable Bundle savedInstanceState) {
    super.onCreateView(savedInstanceState);
    loadPanoImage();
}
4

There are 4 best solutions below

0
On

onCreateView() is called only on fragment if it is an activity you may use onCreate() method. for further details refer this flow diagram of lifecycles.

https://github.com/xxv/android-lifecycle

https://github.com/xxv/android-lifecycle

0
On

onCreate():

You can use onCreate() inside a Fragment, it is called after the Activity's onAttachFragment() but before that Fragment's onCreateView(). In this method, you can assign variables, get Intent extras, and anything else that doesn't involve the View hierarchy. However, you can't use onCreateView() inside an Activity.

onCreateView():

You can assign your View variables and do any graphical initialisations. You are expected to return a root View from this method, and this is the main view, but if your Fragment does not use any layouts or graphics, you can return null.

0
On

For an Activity, we have onCreate
You'd need something like following:

public class MainActivity extends Activity {  

    private VrPanoramaView panoWidgetView

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_pruebafoto);  
        Log.i("lifecycle","onCreate invoked"); 
        panoWidgetView = (VrPanoramaView ) findViewById(R.id.pano_view);
        loadPanoImage(); 
    }  
6
On

Activities use onCreate only. Fragments have both.

onCreate uses setContentView, not an inflater, or super.onCreateView

Your error with this code is that you defined onCreateView twice and the second one (at the bottom), does not Override anything. It needs to return a View, but you have it void. That looks like you tried to define onCreate, but left it as onCreateView.

Delete the second, move loadPanoImage() to the first, before the return.