Skip to content

Commit 81e8a7f

Browse files
committed
life cycle logging
1 parent 17dc76d commit 81e8a7f

3 files changed

Lines changed: 125 additions & 15 deletions

File tree

app/src/main/java/asia/remix/myfragment/FirstFragment.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package asia.remix.myfragment;
22

33
import android.os.Bundle;
4+
import android.util.Log;
45
import android.view.LayoutInflater;
56
import android.view.View;
67
import android.view.ViewGroup;
@@ -11,14 +12,23 @@
1112
import androidx.navigation.fragment.NavHostFragment;
1213

1314
public class FirstFragment extends Fragment{
15+
static final String TAG= "FirstFragment";
16+
17+
@Override
18+
public void onCreate( Bundle savedInstanceState ){
19+
super.onCreate( savedInstanceState );
20+
Log.d( TAG, "onCreate()" );
21+
}
1422

1523
@Override
1624
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ){
25+
Log.d( TAG, "onCreateView()" );
1726
return inflater.inflate( R.layout.fragment_first, container, false );
1827
}
1928

2029
public void onViewCreated( @NonNull View view, Bundle savedInstanceState ){
2130
super.onViewCreated( view, savedInstanceState );
31+
Log.d( TAG, "onViewCreated()" );
2232

2333
Button button = view.findViewById( R.id.button_first );
2434
button.setOnClickListener( new View.OnClickListener(){
@@ -29,9 +39,39 @@ public void onClick( View view ){
2939
} );
3040
}
3141

42+
@Override
43+
public void onStart(){
44+
super.onStart();
45+
Log.d( TAG, "onStart()" );
46+
}
47+
48+
@Override
49+
public void onResume(){
50+
super.onResume();
51+
Log.d( TAG, "onResume()" );
52+
}
53+
54+
@Override
55+
public void onStop(){
56+
super.onStop();
57+
Log.d( TAG, "onStop()" );
58+
}
59+
60+
@Override
61+
public void onPause(){
62+
super.onPause();
63+
Log.d( TAG, "onPause()" );
64+
}
65+
66+
@Override
67+
public void onDestroy(){
68+
super.onDestroy();
69+
Log.d( TAG, "onDestroy()" );
70+
}
71+
3272
@Override
3373
public void onDestroyView(){
3474
super.onDestroyView();
75+
Log.d( TAG, "onDestroyView()" );
3576
}
36-
3777
}

app/src/main/java/asia/remix/myfragment/MainActivity.java

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
package asia.remix.myfragment;
22

33
import android.os.Bundle;
4-
5-
import com.google.android.material.snackbar.Snackbar;
6-
7-
import androidx.appcompat.app.AppCompatActivity;
8-
4+
import android.util.Log;
5+
import android.view.Menu;
6+
import android.view.MenuItem;
97
import android.view.View;
108

9+
import androidx.appcompat.app.AppCompatActivity;
1110
import androidx.appcompat.widget.Toolbar;
1211
import androidx.navigation.NavController;
1312
import androidx.navigation.Navigation;
1413
import androidx.navigation.ui.AppBarConfiguration;
1514
import androidx.navigation.ui.NavigationUI;
1615

17-
import android.view.Menu;
18-
import android.view.MenuItem;
19-
2016
import com.google.android.material.floatingactionbutton.FloatingActionButton;
2117

2218
public class MainActivity extends AppCompatActivity{
23-
24-
private AppBarConfiguration appBarConfiguration;
19+
static final String TAG= "MainActivity";
20+
AppBarConfiguration appBarConfiguration;
2521

2622
@Override
2723
protected void onCreate( Bundle savedInstanceState ){
2824
super.onCreate( savedInstanceState );
29-
25+
Log.d( TAG, "onCreate()" );
3026
setContentView( R.layout.activity_main );
3127
Toolbar toolbar = findViewById( R.id.toolbar );
3228
setSupportActionBar( toolbar );
@@ -39,11 +35,46 @@ protected void onCreate( Bundle savedInstanceState ){
3935
fab.setOnClickListener( new View.OnClickListener(){
4036
@Override
4137
public void onClick( View view ){
42-
Snackbar.make( view, "Replace with your own action", Snackbar.LENGTH_LONG ).setAction( "Action", null ).show();
38+
Log.d( TAG, "onClick()" );
4339
}
4440
} );
4541
}
4642

43+
@Override
44+
public void onStart(){//onCreate → onStart → onResume / onRestart → onStart → onResume
45+
super.onStart();
46+
Log.d( TAG, "onStart()" );
47+
}
48+
49+
@Override
50+
public void onResume(){
51+
super.onResume();
52+
Log.d( TAG, "onResume()" );
53+
}
54+
55+
@Override
56+
public void onPause(){//→ onResume()
57+
super.onPause();
58+
Log.d( TAG, "onPause()" );
59+
}
60+
61+
@Override
62+
public void onStop(){//→ onRestart() → onStart()
63+
super.onStop();
64+
Log.d( TAG, "onStop()" );
65+
}
66+
67+
@Override
68+
public void onDestroy(){//onPause() → onStop() → onDestroy()
69+
super.onDestroy();
70+
Log.d( TAG, "onDestroy()" );
71+
}
72+
73+
@Override
74+
public void onWindowFocusChanged( boolean hasFocus ){
75+
Log.d( TAG, "onWindowFocusChanged()" );
76+
}
77+
4778
@Override
4879
public boolean onCreateOptionsMenu( Menu menu ){
4980
// Inflate the menu; this adds items to the action bar if it is present.

app/src/main/java/asia/remix/myfragment/SecondFragment.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package asia.remix.myfragment;
22

33
import android.os.Bundle;
4+
import android.util.Log;
45
import android.view.LayoutInflater;
56
import android.view.View;
67
import android.view.ViewGroup;
@@ -11,15 +12,23 @@
1112
import androidx.navigation.fragment.NavHostFragment;
1213

1314
public class SecondFragment extends Fragment{
15+
static final String TAG= "SecondFragment";
1416

1517
@Override
16-
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ){
18+
public void onCreate( Bundle savedInstanceState ){
19+
super.onCreate( savedInstanceState );
20+
Log.d( TAG, "onCreate()" );
21+
}
1722

23+
@Override
24+
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ){
25+
Log.d( TAG, "onCreateView()" );
1826
return inflater.inflate( R.layout.fragment_second, container, false );
1927
}
2028

2129
public void onViewCreated( @NonNull View view, Bundle savedInstanceState ){
2230
super.onViewCreated( view, savedInstanceState );
31+
Log.d( TAG, "onViewCreated()" );
2332

2433
Button button = view.findViewById( R.id.button_second );
2534
button.setOnClickListener( new View.OnClickListener(){
@@ -30,9 +39,39 @@ public void onClick( View view ){
3039
} );
3140
}
3241

42+
@Override
43+
public void onStart(){
44+
super.onStart();
45+
Log.d( TAG, "onStart()" );
46+
}
47+
48+
@Override
49+
public void onResume(){
50+
super.onResume();
51+
Log.d( TAG, "onResume()" );
52+
}
53+
54+
@Override
55+
public void onStop(){
56+
super.onStop();
57+
Log.d( TAG, "onStop()" );
58+
}
59+
60+
@Override
61+
public void onPause(){
62+
super.onPause();
63+
Log.d( TAG, "onPause()" );
64+
}
65+
66+
@Override
67+
public void onDestroy(){
68+
super.onDestroy();
69+
Log.d( TAG, "onDestroy()" );
70+
}
71+
3372
@Override
3473
public void onDestroyView(){
3574
super.onDestroyView();
75+
Log.d( TAG, "onDestroyView()" );
3676
}
37-
3877
}

0 commit comments

Comments
 (0)