flutter_rowen
flutter_rowen

Reputation: 53

Android studio Fragment onAttach. What happens in onAttach?

I have been learning about fragments in android studio. Here is what I have been reading Click and Click.

I writed this simple code. When you click a button in a fragment (frag.xml) then text appears (activity_main.xml). Code works fine but I cannot understand what happens in onAttach. Can some explain this onAttach code in a simple way? I get the trycatch and sout message but otherwise I am lost.

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    try{
        st =(setText) context;
    }catch (Exception e){
        System.out.println("Virhe " + e);
    }
}

Here is the rest of the code

import android.content.Context;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;


public class interExample extends Fragment {

setText st;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.frag,container,false);
    Button bt =(Button) view.findViewById(R.id.button);
    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            st.text("fragone pressed");
        }
    });
    return view;
}




public interface setText{
    void text(String text);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    try{
        st =(setText) context;
    }catch (Exception e){
        System.out.println("Virhe " + e);
    }
}

Here is MainActivity

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements interExample.setText{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Fragment f1 = new interExample();
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.line1,f1);
    ft.commit();
}


@Override
public void text(String t){
    TextView tv = (TextView) findViewById(R.id.textview);
    tv.setText(t);
}}

Thank you for the answers!

Upvotes: 0

Views: 1409

Answers (1)

Andy
Andy

Reputation: 469

You can see that your MainActivity implement interExample.setText which is an interface you define in your fragment. This mean your MainActivity will implement the method inside setText interface (define what they do).

Now let's straight to your main question about the onAttach(), this method is called first time a fragment is called. (take a look at fragment lifecycle)

in this onAttach() there are code like

try{
   st =(setText) context;
}catch (Exception e){
   System.out.println("Virhe " + e);
}

here the code is trying to say that we are passing the context to st

to further simplified it would be to pass whichever activity is calling this fragment (context since Activity is a context) to st MainActivity is calling this fragment so MainActivity is st here.

note: the (setText) is just casting the context to setText so that here the activity will act as setText.

So then we could execute method ini MainActivity by usingst, because we already implement them in the MainActivity

Upvotes: 1

Related Questions