Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy on landscape/potrait orientation change fragments get attached before onCreate called on main activity?
    primarykey
    data
    text
    <p>I have main activity which embeds fragment:</p> <pre><code> @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); vd = VirtualDatabaseTableProvider.getInstance(getApplicationContext()); fm = getSupportFragmentManager(); fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); //Create Layout and add fragments setContentView(R.layout.main_window); ListFragment ListFragment= new ListFragment(); android.support.v4.app.FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.fragment_pane, ListFragment, "List"); //ft.replace(R.id.fragment_pane, ListFragment); ft.addToBackStack(null); ft.commit(); //Initialising buttons imgBtnFontInc = (ImageButton) findViewById(R.id.ImgBtnUpFont); imgBtnFontInc.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(textViewAttached){ try{ //Some text Resize } }catch (NullPointerException npe){ Log.e(TAG, "Error on calling Text resize"); Log.e(TAG, npe.getMessage()); Log.e(TAG, npe.getStackTrace().toString()); } } } } ); /* More Buttons code..... */ imgBtnFontDec.setVisibility(View.GONE); imgBtnFontInc.setVisibility(View.GONE); /* Some Saved State handling to recover detailed text Screen*/ if(savedInstanceState != null){ if (savedInstanceState.containsKey("UUID")){ try{ String uuid = savedInstanceState.getString("UUID"); if (uuid != null){ iniTextScreen(uuid); } }catch (Exception e){ Log.e(TAG, "Unable To return text"); } } } </code></pre> <p>Text is initialised with function:</p> <pre><code>private void initTextScreen(String StringID){ Bundle Data = new Bundle(); Data.putString("UUID", StringID); TextScreenFragment TextFragment = new TextScreenFragment(); TextFragment.setArg1ments(Data); if(fm == null){ fm = getSupportFragmentManager(); } android.support.v4.app.FragmentTransaction ft = fm.beginTransaction(); ft.setCustomAnimations( R.anim.animation_enter, R.anim.animation_exit); ft.replace(R.id.fragment_pane, TextFragment, "TextFragment"); ft.addToBackStack(null); ft.commit(); } </code></pre> <p>I handled Buttons visibility in main activity with simple Callback from TextScreenFragment. Callback in main activity:</p> <pre><code>public void onTextViewAttached() { textViewAttached = true; MainActivity.this.imgBtnFontDec.setVisibility(View.VISIBLE); MainActivity.this.imgBtnFontInc.setVisibility(View.VISIBLE); } </code></pre> <p>Callback called in TextScreenFragment:</p> <pre><code> @Override public void onAttach(Activity activity) { super.onAttach(activity); if (!(activity instanceof Callbacks)) { throw new IllegalStateException( "Activity must implement fragment's callbacks."); } else { listener = (Callbacks) activity; listener.onTextViewAttached(); } } public interface Callbacks { /** * Callback for when an item has been selected. */ public void onTextViewAttached(); } </code></pre> <p>It works, however when I put android phone is switch potrait/landscape mode: onAttached in fragment get called way before onCreate in main Activity and Button objects. How can fragment be attached on main activity before even onCreate is called in main activity?</p> <p>I attached a particular fragment at very end of onCreate method after buttons were already initialized,but why onAttach in fragment is called before even I attach fragment and get null exception, because button objects were not initialized in onCreate? How it is even possible?</p> <p>When I comment out:</p> <pre><code> `// MainActivity.this.imgBtnFontDec.setVisibility(View.VISIBLE); //MainActivity.this.imgBtnFontInc.setVisibility(View.VISIBLE);` </code></pre> <p>in callback function <code>public void onTextViewAttached()</code>, no more crashes, but still I noticed that onAttach is called before main activity created and is called twice: one time with uninitialised activity from hell knows were (every element of main activity is either null or has default values), second time when fragment is properly attached from main activity onCreate. </p> <p>I made conclusion that on orientation switch fragments gets atached to uninitialised activity. Am I missing something, on orientation change should I call some other function before onCreate in main activity to get buttons from layout? </p> <p>Is it some kind of fragment automated attach behaviour which I am not aware of and I could take advantage of?</p> <p>What is life cycle of activity with fragment attached to it, because onAttach called in fragment before even main activity is created seems counter intuitive.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload