T.Y.BSc(I.
T) SEM-VI Maharashtra College Advanced Mobile Programming
Practical 3
Programming Activities and fragments
Activity Life Cycle, Activity methods, Multiple Activities, Life Cycle of
fragments and multiple fragments.
a) Activity Life Cycle
The various methods to be created are onStart(), onRestart(), onStop(),
onResume(),onDestroy() and onPause() as shown in the figure.
Create a new project and go to, MainActivity.java
Prof. Ansari Mohd. Shahid(7977-079-345 / 9821-77-1054) Page 1
T.Y.BSc(I.T) SEM-VI Maharashtra College Advanced Mobile Programming
package MaharashtraCollege.example.profshahidansari;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
String tag= "Android Lifecycle Demonstration";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(tag,"In the onCreate() event");
}
public void onStart()
{
super.onStart();
Log.d(tag,"In the onStart() event");
}
public void onRestart()
{ super.onRestart();
Log.d(tag,"In the onRestart() event");
}
public void onPause()
{
super.onPause();
Log.d(tag,"In the onPause() event");
}
public void onStop()
{
super.onStop();
Log.d(tag,"In the onStop() event");
}
public void onDestroy()
{
super.onDestroy();
Log.d(tag,"In the onDestroy() event");
}
Prof. Ansari Mohd. Shahid(7977-079-345 / 9821-77-1054) Page 2
T.Y.BSc(I.T) SEM-VI Maharashtra College Advanced Mobile Programming
Output
See the Output in Run tab
b) Life Cycle of fragments
Create a new project
Add a new Fragment
ProjectName>App>src>main>Java
3. Right click on Java and select ‘Add Fragment’>Add
Fragment(Blank)
Give a name to the fragment and click Finish
Prof. Ansari Mohd. Shahid(7977-079-345 / 9821-77-1054) Page 3
T.Y.BSc(I.T) SEM-VI Maharashtra College Advanced Mobile Programming
Prof. Ansari Mohd. Shahid(7977-079-345 / 9821-77-1054) Page 4
T.Y.BSc(I.T) SEM-VI Maharashtra College Advanced Mobile Programming
package MaharashtraCollege.example.profshahidansari;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.util.Log;
public class LifeCycleofFragment extends Fragment {
String tag = "Life Cycle of Fragment";
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public LifeCycleofFragment() {
// TODO: Rename and change types and number of parameters
public static LifeCycleofFragment newInstance(String param1, String
param2) {
LifeCycleofFragment fragment = new LifeCycleofFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Prof. Ansari Mohd. Shahid(7977-079-345 / 9821-77-1054) Page 5
T.Y.BSc(I.T) SEM-VI Maharashtra College Advanced Mobile Programming
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_life_cycleof, container,
false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
public void onStart()
{
super.onStart();
Log.d(tag,"In the onStart() event");
}
public void onRestart()
{
Log.d(tag,"In the onRestart() event");
}
public void onPause()
{
super.onPause();
Log.d(tag,"In the onPause() event");
}
public void onStop()
{
super.onStop();
Log.d(tag,"In the onStop() event");
}
Prof. Ansari Mohd. Shahid(7977-079-345 / 9821-77-1054) Page 6
T.Y.BSc(I.T) SEM-VI Maharashtra College Advanced Mobile Programming
public void onDestroy()
{
super.onDestroy();
Log.d(tag,"In the onDestroy() event");
}
}
Output
Prof. Ansari Mohd. Shahid(7977-079-345 / 9821-77-1054) Page 7