0

I am having some problems to understand the differences between Activity and Fragment.

I have done an activity called "PublicarActivity" and a Fragment called "PublicarFragment".

They have exactly the same code (with some differences to work as a fragment and as an activity) so that is not a problem.

My problem is that I do not really know how to work with "onBackPressed". I know that before than calling the fragment, you should add it to the stack, but right now I would like to do something a little bit more complicated.

This is the code for my Activity's onBackPressed:

@Override
public void onBackPressed() {
    if(layout_activado){
        verificable.toggle();
        verificar_layout.setVisibility(View.INVISIBLE);
        layout_activado = false;
        pulsado = false;        }
    else{
        Intent intent_cancelar = new Intent(PublicarActivity.this, Principal_Activity.class);
        startActivity(intent_cancelar);
    }
}

How could I do exactly this from my fragment?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
  • 3
    Have you seen this? http://stackoverflow.com/questions/5448653/how-to-implement-onbackpressed-in-fragments – OneCricketeer Dec 08 '16 at 16:00
  • how can i share the fragment's variables with my activity? I mean, what I have understood, is that I should code onBackPressed() on my activity, to use it from my fragment. Am I right? – Mario López Batres Dec 08 '16 at 16:09
  • The Fragment class has no onBackPressed method. The Activity contains the Fragment, and is what responds to the pressing of the back button, yes. I'm not sure which variables you are referring to. – OneCricketeer Dec 08 '16 at 16:12
  • It are basically some booleans that I have created in my Fragment. If I create the same ones in my activity, would I work? – Mario López Batres Dec 08 '16 at 16:16
  • That depends what you need those booleans to do. You can always get the values from within the Fragment by using `((PublicarActivity) getActivity()).getYourBoolean()`, for example – OneCricketeer Dec 08 '16 at 16:33

1 Answers1

0

There are two things in your question to be solved to get you the answer.

First thing is confusion between Activity and Fragment. You might have encountered an statement -"Activity represents single screen" in Android. So having Activity in your application will let your user interact with various views such as buttons, lists etc. So now, let's consider an instance when you want to add such a view in your Activity which should contain some state lifecycle (like you can have list in fragment and clicking on item should lead you to detailed view in the same view) so that you can have mini-Activity in your main activity while all other components remaining at the same positions. So providing functionalities like mini-activity your Fragment is going to have some life-cycle methods which will be called during Fragment Life time. So you can use Fragment whenever you feel you want some sub-Activity in your main Activity or for any other use. You can cover your whole Activity with Fragment as we mostly do whenever we want to have Navigation-Drawer in our app.

Now that you have got clear about Fragment and Activity( I hope so) you can refer to the link provided by person named cricket which is this.

Community
  • 1
  • 1
Dushyant Suthar
  • 673
  • 2
  • 9
  • 27