3

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();
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Javi
  • 41
  • 1
  • 7
  • You should research "Activity lifecycle" ... It clearly shows what methods are loaded. Then compare with "Fragment lifecycle" – OneCricketeer Jan 05 '18 at 05:54
  • Please [edit] your question to include what error you get. "I need help, it doesn't work, can you help, etc?" is not a question, so please leave them out of posts. Thanks! – OneCricketeer Jan 05 '18 at 05:59
  • 1
    Are you not aware of activity and fragment lifecycle.How can you move to development. – Shivam Oberoi Jan 05 '18 at 06:02

4 Answers4

1

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

Sneh Pandya
  • 8,197
  • 7
  • 35
  • 50
Chanaka Fernando
  • 2,176
  • 19
  • 19
1

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.

Ege Kuzubasioglu
  • 5,991
  • 12
  • 49
  • 85
0

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(); 
    }  
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
mibrahimy
  • 722
  • 4
  • 18
0

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.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • I already deleted the Override at the bottom and move the loadPanoImage() to the first Override before the return. I changed onCreateActivity for onCreate, but the Override still gives me error. – Javi Jan 05 '18 at 06:25
  • Again, Activity has no `onCreateView`, so no, there is nothing to override, and that will be an error – OneCricketeer Jan 05 '18 at 06:27
  • Override public View onCreate(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); loadPanoImage(); return v; } – Javi Jan 05 '18 at 06:34
  • Your method is named onCreate now... It must be `onCreateView`, which needs to be in a Fragment – OneCricketeer Jan 05 '18 at 06:35
  • 1
    OK, now I have an Activity and I am using OnCreate() and the panoWidgetView. Now the app builds succesfully, but when I try it in the emulator everything works fine, except it does not load the image in the PanoView (the app crashes when I go to the Activity where the Panowidget is. – Javi Jan 05 '18 at 21:02
  • @Javi Please post a new question that includes your Logcat, not a comment here. We've answered the existing error. – OneCricketeer Jan 05 '18 at 22:28