In my application, On the MainActivity.java I need to display two layouts, One is visible in the initial stage and the other is invisible. When a button is pressed the invisible one get visible and visible one get invisible, here is the code i have writen:
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
SupportMapFragment supportMapFragment;
SupportMapFragment supportMapFragment1;
FusedLocationProviderClient client;
View checkoutLayout, checkinLayout;
ImageButton checking_button, checkout_button;
Chronometer chronometer;
Handler handler;
long tMillisec, tStart, tBuff, tUpdate = 0L;
int sec, min, millisec, hr;
TextView tv_location, editText_remark, location_checkout, location_remark, dateView, timeView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
String date = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(new Date());
String currentTime = new SimpleDateFormat("HH:mm", Locale.getDefault()).format(new Date());
checking_button = findViewById(R.id.checkin_btn);
chronometer = findViewById(R.id.chrono);
checkoutLayout = findViewById(R.id.checkOut);
checkinLayout = findViewById(R.id.checkinLayout);
checkout_button = findViewById(R.id.checkout_btn);
tv_location = findViewById(R.id.tv_location);
editText_remark = findViewById(R.id.editText_remark);
location_checkout = findViewById(R.id.location_checkout);
location_remark = findViewById(R.id.Remark_checkout);
dateView = findViewById(R.id.date_pick);
timeView = findViewById(R.id.time_pick);
checkinLayout.setVisibility(View.VISIBLE);
checkoutLayout.setVisibility(View.INVISIBLE);
handler = new Handler();
drawerLayout = findViewById(R.id.drawer_layout);
supportMapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map_view);
supportMapFragment1 = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_view1);
client = LocationServices.getFusedLocationProviderClient(this);
// check permission
if(ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
// when permission granted call method
getCurrentLocation();
}else{
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 44);
}
checking_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(tv_location.getText().toString().isEmpty() || editText_remark.getText().toString().isEmpty()){
Toast.makeText(getApplicationContext(), "Please Enter your Location and Remark", Toast.LENGTH_SHORT).show();
}else {
checkoutLayout.setVisibility(v.VISIBLE);
checkinLayout.setVisibility(v.GONE);
tStart = SystemClock.uptimeMillis();
handler.postDelayed(runnable, 0);
chronometer.start();
location_checkout.setText(tv_location.getText().toString());
location_remark.setText(editText_remark.getText().toString());
dateView.setText("Date:"+ date);
timeView.setText("Punch-In:"+ currentTime);
}
}
});
checkout_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkinLayout.setVisibility(v.VISIBLE);
checkoutLayout.setVisibility(v.GONE);
}
});
}
The code works good but the problem is when i make checkoutLayot visible and checkinLayout invisible, when i close the app and reopen it the layout changes to checkoutLayout invisible and checkinLayout visible, So i need to make the last visited page visible. I am new to android development, can any one help me to solve the problem.
2
Answers
You have to use
SharePreference
for this, to save the state that your app was in when the user closed the app. Save the state in the sharedpreference as soon as user presses a button and then inonStart
method of the app, you can get the value of the state from theSharePreference
and then show your layouts accordingly.First thing first you should put all your variables including views(like Textview, Handler etc. private). Secondly our friend @rootass here is right you should use
SharePrference
so you can save changes. Here is an example of writing and gettingSharedPreferences
. Code for Wrting SharedPreferences …And here code for reading SharedPreferences…
ps : since you are new to android i probably should mention that instead of
context
in mentioned code above if you are in independtend activity you do not need to use context just putthis
or nothing and of you are not in an independent activity, in case of fragment usecontext
orrequireActivity()
and in case of being in recyclerview use passedcontext
.Hope it helps you 😉