11-Ma’ruza Mavzu: Resurslar va fragmentlar. Reja: Resurslar bilan ishlash
Download 345.81 Kb.
|
11-maruza
- Bu sahifa navigatsiya:
- MainActivity
- @ [paket_nomi:] resurs_tipi / resurs_nomi paket_nomi
- RESUMED
onViewStateRestored(): fragmentning ko'rish holatini oling. Bu usul bajarilgandan so'ng, fragment ko'rinishi CREATED holatiga o'tadi
public void onViewStateRestored (Bundle savedInstanceState) onStart(): fragment ko'rinib turganda va ko'rish bilan birga STARTED holatiga kirganda chaqiriladi public void onStart () onResume(): Fragment faollashganda va foydalanuvchi u bilan muloqot qila olganda chaqiriladi. Bunday holda, fragment va uning taqdimoti RESUMED holatiga o'tadi public void onResume () onPause(): fragment ko'rinishda qolaveradi, lekin u endi faol emas va ko'rinish bilan birgalikda STARTED holatiga o'tadi. public void onPause () onStop(): fragment endi ko'rinmaydi va ko'rinish bilan birga CREATED holatiga o'tadi public void onStop () Hayot tsiklining bu nuqtasida biz onSaveInstanceState() usuli yordamida fragment holatini saqlashimiz mumkin. Ammo shuni yodda tutish kerakki, bu usulni chaqirish API versiyasiga bog'liq. API 28dan oldin onSaveInstanceState() onStop()dan oldin chaqiriladi va onStop() dan keyin API 28 boshlanadi. onDestroyView(): fragment ko'rinishi yo'q qilingan. Ko'rinish DESTROYED holatiga o'tadi onDestroy(): fragmentni butunlay yo'q qilish - u ham DESTROYED holatiga o'tadi FragmentManagerdan fragment o'chirilganda va Activity sinfidan ajratilganda onDetach() usuli chaqiriladi. Bu usul hayot aylanishining boshqa barcha usullaridan keyin chaqiriladi. Fragment sinfining kodida biz bu usullarning hammasini yoki bir qismini bekor qilishimiz mumkin. Masalan, bizda quyidagi loyiha aniqlangan deylik. res/layout katalogi layout faylini fragment_content.xml uchun quyidagi belgi bilan belgilaydi: xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> Fragment sinfi ushbu faylni ko'rinishni o'rnatish uchun ishlatadi, shuningdek hayot aylanishini boshqarish usullarini belgilaydi: package com.example.fragmentapp; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import android.util.Log; import java.util.Date; public class ContentFragment extends Fragment { private final static String TAG = "ContentFragment"; public ContentFragment(){ Log.d(TAG, "Constructor"); } @Override public void onAttach(@NonNull Context context) { super.onAttach(context); Log.d(TAG, "onAttach"); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "onCreate"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d(TAG, "onCreateView"); return inflater.inflate(R.layout.fragment_content, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); Button updateButton = (Button) view.findViewById(R.id.updateButton); TextView updateBox = (TextView) view.findViewById(R.id.dateTextView); updateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String curDate = new Date().toString(); updateBox.setText(curDate); } }); Log.d(TAG, "onViewCreated"); } @Override public void onViewStateRestored(@Nullable Bundle savedInstanceState) { super.onViewStateRestored(savedInstanceState); Log.d(TAG, "onViewStateRestored"); } @Override public void onStart() { super.onStart(); Log.d(TAG, "onStart"); } @Override public void onResume() { super.onResume(); Log.d(TAG, "onResume"); } @Override public void onPause() { super.onPause(); Log.d(TAG, "onPause"); } @Override public void onStop() { super.onStop(); Log.d(TAG, "onStop"); } @Override public void onDestroyView() { super.onDestroyView(); Log.d(TAG, "onDestroyView"); } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy"); } @Override public void onDetach() { super.onDetach(); Log.d(TAG, "onDetach"); } } Fragment yaratishga qaragan oldingi maqoladan farqli o'laroq, bu erda fragment ko'rinishni onCreateView usulida o'rnatadi. Buning uchun l_ayout manba identifikatori va konteyner - fragment yuklanadigan ViewGroup ob'ekti LayoutInflater ob'ektining inflate() usuliga o'tkaziladi. Natijada, inflate () usuli yaratilgan ko'rinishni qaytaradi. return inflater.inflate(R.layout.fragment_content, container, false); Download 345.81 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling