Camposha
  • Home
  • Android
  • Buy Projects
  • My Account
    • Become a Member
    • Log In|Log Out
    • Free Projects
No Result
View All Result
Camposha
  • Home
  • Android
  • Buy Projects
  • My Account
    • Become a Member
    • Log In|Log Out
    • Free Projects
No Result
View All Result
Camposha
No Result
View All Result
Home Android Activity Activity Basics

Activity State Changes

Handling Activity State changes

1 week ago
in Activity Basics
39 1
A A
40
SHARES
4k
VIEWS
Share on FacebookShare on Twitter

SimilarPosts

Introduction to Activities

1 week ago
4k
LessonExample

Android Activity State change handling example:

Activity STate changes
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import android.content.res.Configuration;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
 
    public static final String TAG = "Android Activity State";
 
    // view
    private EditText etString;
    private TextView tvOutput;
 
    // string to be saved
    private String myString;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); // Always call the superclass first
        setContentView(R.layout.activity_main);
        bindView();
 
        // No need to use this if we implement onRestoreInstanceState
        // Check whether we're recreating a previously destroyed instance
//        if (savedInstanceState != null) {
//            // Restore value of members from saved state
//            //myString = savedInstanceState.getString("mySavedString");
//            Log.d(TAG, "onCreate() called with: savedInstanceState = [" + savedInstanceState + "]");
//            Log.d(TAG, "onCreate() called with: myString = [" + myString + "]");
//        }
//       else {
//            // Probably initialize members with default values for a new instance
//            myString = null;
//            Log.d(TAG, "onCreate() called with: myString = [" + myString + "]");
//        }
    }
 
    private void bindView(){
        etString = (EditText) findViewById(R.id.et_string);
        tvOutput = (TextView) findViewById(R.id.tv_output);
    }
 
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // Always call the superclass so it can save the view hierarchy state
        super.onSaveInstanceState(outState);
 
        // Save UI state changes to the savedInstanceState.
        // This bundle will be passed to onCreate if the process is
        // killed and restarted.
 
        //Format: outState.putVarType("VarName", "VarValue");
        outState.putString("mySavedString", myString);
        Log.d(TAG, "onSaveInstanceState() called with: outState = [" + outState + "]");
    }
 
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // Always call the superclass so it can restore the view hierarchy
        super.onRestoreInstanceState(savedInstanceState);
 
        // Restore UI state from the savedInstanceState.
        // This bundle has also been passed to onCreate.
 
        // Format: VarType varName = savedInstanceState.getVarType("VarName");
        myString = savedInstanceState.getString("mySavedString");
        Log.d(TAG, "onRestoreInstanceState() called with: savedInstanceState = [" + savedInstanceState + "]");
        Log.d(TAG, "onRestoreInstanceState() called with: myString = [" + myString + "]");
    }
 
    public void process(View view){
        switch (view.getId()) {
            case R.id.btn_save:
                myString = etString.getText().toString();
                etString.setText("");
                etString.setHint("Type something here");
                break;
            case R.id.btn_show:
                if (myString != null)
                    tvOutput.setText(myString);
                else
                    tvOutput.setText("No saved string.");
                break;
            case R.id.btn_clear:
                resetView();
                break;
        }
    }
 
    private void resetView(){
        etString.setText("");
        etString.setHint("Type something here");
        tvOutput.setText("");
    }
 
    // called when the activity is about to become visible
    @Override
    protected void onStart() {
        super.onStart();
        //Log.d(TAG, "onStart() called");
    }
 
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart() called");
    }
 
    // called when the activity has become visible
    @Override
    protected void onResume() {
        super.onResume();
        //resetView();
        Log.d(TAG, "onResume() called");
    }
 
    // called when another activity is taking focus
    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause() called");
    }
 
    // called when the activity is no longer visible
    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop() called");
    }
 
    // called just before the activity is destroyed
    // Configuration changes during runtime (screen orientation, keyboard availability, and language) also call onDestroy
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy() called");
    }
}

 

Share16Tweet10Send

Related Posts

Activity Basics

Introduction to Activities

February 16, 2021
4k

Categories

  • Android
    • Introduction
    • Activity
      • Activity Basics
      • Lifecycle
      • Routing
    • Fragment
    • UI
      • AppBar and Toolbar
      • TextView
      • Tabs
      • Dialog
      • CardView
      • Chips
      • EditText
      • Navigation Drawer
      • Button
      • Bottom Navigation
    • Animation
    • MediaPlayer
      • VideoPlayer
    • Biometric
    • Jetpack
    • Android Firebase
    • Layouts
    • JSON
    • ContentViews
    • RxJava
    • PinView
    • Utility
    • ScrollView
    • URL
    • Log
    • Intent
    • DateTime
    • DownloadManager
    • Camera
    • SharedPreferences
    • SQLite
    • Retrofit
    • RecyclerView
    • Bluetooth
    • Hardware and Sensors
    • HTTP
    • Android AdapterViews
    • Projects
    • InputViews
    • Navigation
    • Menu
    • System
    • Communication
    • HttpURLConnection
    • Fast Networking Library
    • FileSystem
    • Components
    • Threading
    • Local Database
    • Jetpack
  • Flutter
    • Flutter HTTP
    • Flutter Widgets
    • Flutter Firebase
  • Java
    • Java Threading
    • Java Collections
    • JavaFX
  • Programming
  • Flutter Projects
  • Java DateTime
  • Flutter Listing Views
  • Flutter Image
  • Flutter Local Database
  • Documentary
  • Meditation
  • Flutter Animation and Motion
  • Javascript
    • Framework7
  • Xamarin Android
  • Python
    • CherryPy

Get Free Projects

  • Home
  • Android
  • Buy Projects
  • My Account

© 2021 Camposha

No Result
View All Result
  • Account
  • Activate
  • Activity
  • Become a Teacher
  • Become a Teacher
  • Become a Teacher
  • Become instructor
  • Blog
  • Blog
  • Cancel Payment
  • Cancel Payment
  • Cart
  • Change Password
  • Change Password
  • Checkout
  • Checkout
  • Checkout
  • Contact
  • Contact
  • Contact Us
  • Content restricted
  • Course Checkout
  • Dashboard
  • Edit Profile
  • Edit Profile
  • FAQs
  • Forgot Password
  • Forgot Password
  • Guest
  • Guest
  • Home
  • Home
  • Home Light
  • Instructor Dashboard
  • Instructor Registration
  • IUMP – Account Page
  • IUMP – Default Redirect Page
  • IUMP – Login
  • IUMP – LogOut
  • IUMP – Register
  • IUMP – Reset Password
  • IUMP – TOS Page
  • IUMP – Visitor Inside User Page
  • List courses
  • List wish list
  • Login
  • Login
  • Maintenance
  • Members
  • Membership Account
    • Membership Billing
    • Membership Cancel
    • Membership Checkout
    • Membership Confirmation
    • Membership Invoice
    • Membership Levels
  • Membership Account
    • Membership Billing
    • Membership Cancel
    • Membership Checkout
    • Membership Confirmation
    • Membership Invoice
    • Membership Levels
  • Membership Plans
  • My Account
  • OnePage Documentation
  • Portfolio Grid
  • Portfolio Masonry
  • Portfolio Multigrid
  • Privacy Policy
  • Products
  • Profile
  • Profile
  • Profile
  • Projects
  • Register
  • Register
  • Register
  • Register
  • Sample Page
  • Shop
  • Sign in
  • Sign up
  • Student profile
  • Student Registration
  • Thank You
  • Thank You

© 2021 Camposha

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms below to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In