When I run my application on the emulator, it run right and no problem happens. But when I run it on the phone, a problem appear "stop unexpectedly".
In my application:
The main activity (Background) can exchange to 4 activities: LivingRoom, DiningRoom, Wc, Garage. The problem "stop unexpectedly" just happens when i try to go to the DiningRoom activity.
This is my code :
1.Background
package com.example.background;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import at.abraxas.amarino.Amarino;
public class BackgroundActivity extends Activity {
private Button Wc, Dining_Room, Living_Room, Garage;
private Intent D_intent, L_intent, G_intent, W_intent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Amarino.connect(this, DEVICE_ADDRESS);
Living_Room = (Button) findViewById(R.id.living);
//Living_Room.setBackgroundColor(Color.TRANSPARENT);
Living_Room.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
L_intent = new Intent(view.getContext(), LivingRoom.class);
startActivityForResult(L_intent, 0);
}
});
Dining_Room = (Button) findViewById(R.id.dining);
// Dining_Room.setBackgroundColor(Color.TRANSPARENT);
Dining_Room.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
D_intent = new Intent(view.getContext(), DiningRoom.class);
startActivityForResult(D_intent, 0);
}
});
Wc = (Button) findViewById(R.id.wc);
//Wc.setBackgroundColor(Color.TRANSPARENT);
Wc.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
W_intent = new Intent(view.getContext(), Wc.class);
startActivityForResult(W_intent, 0);
}
});
Garage = (Button) findViewById(R.id.garage);
// Garage.setBackgroundColor(Color.TRANSPARENT);
Garage.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
G_intent = new Intent(view.getContext(), Garage.class);
startActivityForResult(G_intent, 0);
}
});
}
}
2.DiningRoom
package com.example.background;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.ToggleButton;
import at.abraxas.amarino.Amarino;
public class DiningRoom extends Activity implements OnCheckedChangeListener, OnSeekBarChangeListener{
private static final String DEVICE_ADDRESS = "00:06:66:43:9B:56";
final int DELAY = 150;
ToggleButton button;
SeekBar seekbar1;
SeekBar seekbar2;
boolean light2;
int light1;
int fan;
long lastchange;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.diningroom);
//back ve trang chinh
Button backhome = (Button) findViewById(R.id.D_backhome);
backhome.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
Amarino.connect(this, DEVICE_ADDRESS);
button = (ToggleButton) findViewById(R.id.Dbutton);
seekbar1 = (SeekBar) findViewById(R.id.Dseekbar1);
seekbar2 = (SeekBar) findViewById(R.id.Dseekbar2);
button.setOnCheckedChangeListener(this);
seekbar1.setOnSeekBarChangeListener(this);
seekbar2.setOnSeekBarChangeListener(this);
}
//---START----------------------------------------------------------------------------------------------
#Override
protected void onStart(){
super.onStart();
// load last state
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
light2 = pref.getBoolean("light2", true);
light1 = pref.getInt("light1", 0);
fan = pref.getInt("fan", 0);
button.setChecked(light2);
seekbar1.setProgress(light1);
seekbar2.setProgress(fan);
new Thread(){
public void run(){
try{
Thread.sleep(6000);
}catch (InterruptedException e) {}
update_light2();
update_light1_fan();
}
}.start();
}
//STOP--------------------------------------------------------------------------------
#Override
protected void onStop(){
super.onStop();
PreferenceManager.getDefaultSharedPreferences(this)
.edit()
.putBoolean("light2", light2)
.putInt("light1", light1)
.putInt("fan", fan)
.commit();
Amarino.disconnect(this, DEVICE_ADDRESS);
}
//UPDATE LIGHT2--------------------------------------------------------------------------------
private void Update_Light2_State(final CompoundButton button){
light2 = button.isChecked();
update_light2();
}
private void update_light2(){
int a;
a = (light2 == true)? 255:0;
Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'o', a);
}
//UPDATE LIGHT1 FAN-------------------------------------------------------------------------------------
private void Update_Light1_Fan_State(final SeekBar seekbar){
switch (seekbar.getId()){
case R.id.Dseekbar1:
light1 = seekbar.getProgress();
update_light1();
break;
case R.id.Dseekbar2:
fan = seekbar.getProgress();
update_fan();
break;
}
}
private void update_light1_fan(){
update_light1();
update_fan();
}
private void update_light1(){
Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'p', light1);
}
private void update_fan(){
Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'q', fan);
}
//---TRACE--------------------------------------------------------------------------------------
#Override
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
if (System.currentTimeMillis() - lastchange > DELAY ){
Update_Light2_State(button);
lastchange = System.currentTimeMillis();
}
}
#Override
public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {
if (System.currentTimeMillis() - lastchange > DELAY ){
Update_Light1_Fan_State(seekbar);
lastchange = System.currentTimeMillis();
}
}
#Override
public void onStartTrackingTouch(SeekBar seekbar) {
lastchange = System.currentTimeMillis();
}
#Override
public void onStopTrackingTouch(SeekBar seekbar) {
Update_Light1_Fan_State(seekbar);
}
}
3.Logcat
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.background/com.example.background.DiningRoom}: java.lang.ClassCastException:
java.lang.Boolean
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685)
at android.app.ActivityThread.access$2300(ActivityThread.java:126)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4633)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: java.lang.Boolean
at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2721)
at com.example.background.DiningRoom.onStart(DiningRoom.java:80)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
at android.app.Activity.performStart(Activity.java:3781)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2642)
... 11 more
Force finishing activity com.example.background/.DiningRoom
Force finishing activity com.example.background/.BackgroundActivity
I also try to wrote another application similar to the above application. But I just have only 1 sub activity: DiningRoom, then it ran well on the phone.
The Background when I just have one sub activity
[CODE]
public class Test1Activity extends Activity {
private Button Dining_Room;
private Intent D_intent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Dining_Room = (Button) findViewById(R.id.dining);
// Dining_Room.setBackgroundColor(Color.TRANSPARENT);
Dining_Room.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
D_intent = new Intent(view.getContext(), Dining.class);
startActivityForResult(D_intent, 0);
}
});
}
}
[/CODE]
Careful reading of logcat reveals that:
Caused by: java.lang.ClassCastException: java.lang.Boolean
at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2721)
at com.example.background.DiningRoom.onStart(DiningRoom.java:80)
This means, that shared preference was boolean instead of int on line 80.
PS: I develop lightweight dependency injection framework for android, and it already covers
loading / saving preferences. Just grab it here:
https://github.com/ko5tik/andject
I would guess that the value "light1" in your shared preferences was not saved as an integer (but as a boolean).
From the docs:
Throws ClassCastException if there is a preference with this name that
is not an int.
Related
I have a medicine_activity class
package com.example.shubhmgajra.medikit;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class Medicine_Activity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "Medicine_Activity";
private Button btnAdd;
private ListView listMeds;
private AppDatabase db;
private ArrayAdapter<MedicineRecord> arrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_medicine);
btnAdd = findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(this);
listMeds = findViewById(R.id.listMeds);
listMeds.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MedicineRecord medicineRecord = db.medicineRecordDao().findMedicineById(id + 1);
Intent intent = new Intent(getApplicationContext(), MedicineInputActivity.class);
startActivity(intent);
MedicineInputActivity.getInstance().update(medicineRecord, true);
}
});
db = AppDatabase.getInstance(getApplicationContext());
FeedAdapter feedAdapter = new FeedAdapter(Medicine_Activity.this, R.layout.list_record, db.medicineRecordDao().loadAllRecords());
listMeds.setAdapter(feedAdapter);
}
#Override
protected void onResume() {
super.onResume();
}
#Override
public void onClick(View v) {
Intent intent = new Intent(this, MedicineInputActivity.class);
startActivity(intent);
}
}
In this i am trying to call update() method of another activity namely MedicineInputActivity
package com.example.shubhmgajra.medikit;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;
public class MedicineInputActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MedicineInputActivity";
static EditText dosage;
static EditText medName;
static TimePicker timePicker;
static Button btnSave;
Button btnInc;
Button btnDec;
private AppDatabase db;
private static MedicineInputActivity instance;
boolean isEdit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_medicine_input);
isEdit = false;
instance = this;
dosage = findViewById(R.id.dosage);
dosage.setShowSoftInputOnFocus(false);
dosage.setCursorVisible(false);
medName = findViewById(R.id.medName);
timePicker = findViewById(R.id.simpleTimePicker);
btnInc = findViewById(R.id.btnInc);
btnDec = findViewById(R.id.btnDec);
btnInc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String val = dosage.getText().toString();
int tempVal = Integer.parseInt(val);
tempVal++;
dosage.setText("" + tempVal);
}
});
btnDec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String val = dosage.getText().toString();
int tempVal = Integer.parseInt(val);
if (tempVal > 0) {
tempVal--;
}
dosage.setText("" + tempVal);
}
});
btnSave = findViewById(R.id.btnSave);
btnSave.setOnClickListener(this);
db = AppDatabase.getInstance(getApplicationContext());
}
#Override
public void onClick(View v) {
String name = medName.getText().toString();
int min = timePicker.getMinute();
int hour = timePicker.getHour();
String val = dosage.getText().toString();
int dose = Integer.parseInt(val);
MedicineRecord medicineRecord = new MedicineRecord(name, dose, min, hour);
if (validate(name)) {
if (isEdit) {
db.medicineRecordDao().updateRecord(medicineRecord);
} else {
db.medicineRecordDao().insertRecord(medicineRecord);
}
Intent intent = new Intent(this, Medicine_Activity.class);
startActivity(intent);
} else {
Toast toast = Toast.makeText(getApplicationContext(), "Medicine name cannot be empty", Toast.LENGTH_SHORT);
toast.show();
}
}
private boolean validate(String name) {
if (name.length() == 0) {
return false;
} else {
return true;
}
}
public static MedicineInputActivity getInstance() {
return instance;
}
public void update(MedicineRecord medicineRecord, boolean isEdit) {
this.isEdit = isEdit;
dosage.setText(medicineRecord.getDosage());
medName.setText(medicineRecord.getMedName());
timePicker.setHour(medicineRecord.getHour());
timePicker.setMinute(medicineRecord.getMinute());
}
}
What i am trying to achieve is when the user taps the list item, the input activity is loaded and the user can update the record. I wasnt able to find an elegant solution other than making an update method in MedicineInputActivity and then getting an instance of MedicineInputActivity in Medicine_Activity and calling update.
I am getting errors like "Attempt to invoke virtual method 'void com.example.shubhmgajra.medikit.MedicineInputActivity.update() on a null object reference".
You can return data from second activity to the first activity when user is done updating the record. You should start second activity by calling startActivityForResult() instead of startActivity() in the first activity. You should also override onActivityResult() in the first activity. You can read more here.
UPDATE:
I think you want to send selected medicine details from Medicine_Activity to MedicineInputActivity. In that case, you can send selected medicine object by binding it to the intent as follows:
public class Medicine_Activity ... {
#Override
protected void onCreate(Bundle savedInstanceState) {
...
listMeds.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MedicineRecord medicineRecord = db.medicineRecordDao().findMedicineById(id + 1);
Intent intent = new Intent(getApplicationContext(), MedicineInputActivity.class);
intent.putExtra("selectedMedicine", medicineRecord);
startActivity(intent);
MedicineInputActivity.getInstance().update(medicineRecord, true);
}
});
...
}
}
Then on MedicineInputActivity, you should be doing this:
public class MedicineInputActivity ... {
MedicineRecord selectedMedicineRecord;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
selectedMedicineRecord = (MedicineRecord) getIntent().getParcelableExtra("selectedMedicine");
...
}
}
To be able to do this, your MedicineRecord class should implement Parcelable interface.
This is the line i want to repeat.
handler.postDelayed(runnableCode, 1);
This app is a tycoon app and so the user can buy upgrades and if they tap the button the get $$ and so when they buy upgrades keeping the value up-to-date is required.
package com.example.navjeevenmann.mytycoon;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button myButton;
private int Counter = 0;
private Button myButton2;
private TextView myTextView;
Handler handler = new Handler();
private int Test = 5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler.postDelayed(runnableCode, 1);
myButton = (Button) findViewById(R.id.button);
myButton2 = (Button) findViewById(R.id.button2);
myTextView = (TextView) findViewById(R.id.textView);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ButtonCounter(Counter);
}
});
myButton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),
SecondActivity.class);
intent.putExtra("Count", Counter);
startActivity(intent);
finish();
}
});
}
public int ButtonCounter(int Counter){
Counter+=1;
return Counter;
}
public int AutoCounter(int Counter, int add) {
Counter+=add;
return Counter;
}
public void Display(int Counter, TextView myTextView) {
String man = String.valueOf(Counter);
myTextView.setText("$" + man);
}
private Runnable runnableCode = new Runnable() {
#Override
public void run() {
// Do something here on the main thread
Counter = AutoCounter(Counter,Test);
Display(Counter, myTextView);
}
};
}
handler.postDelayed(runnableCode, 1);
call that line inside onResume() method. Make sure your show an AlertDialog to the user whenever he or she hits the button to get $$. Since the AlertDialog pops up everytime the user tries to buy $$. onResume() will be called.
Try this
private void startAnimation() {
countDownTimer = new CountDownTimer(700, 500) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
//your code to repeat
}
start();
}
}.start();
}
I have a button that goes from one activity to another activity and its supposed to save the value int Counter, but every time it never saves. What am I doing wrong? I have seen other solutions, but i check and im doing the exact same thing as them.
package com.example.navjeevenmann.mytycoon;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button myButton;
private int Counter;
private Button myButton2;
private TextView myTextView;
Handler handler = new Handler();
private int add;
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
add = savedInstanceState.getInt("Count");
Counter = savedInstanceState.getInt("Add");
}
myButton = (Button) findViewById(R.id.button);
myButton2 = (Button) findViewById(R.id.button2);
myTextView = (TextView) findViewById(R.id.textView);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Counter = ButtonCounter(Counter);
}
});
myButton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("Count", Counter);
startActivity(intent);
}
});
handler.postDelayed(new Runnable() {
#Override
public void run() {
Counter = AutoCounter(Counter, add);
Display(Counter, myTextView);
handler.postDelayed(this, 100);
}
}, 10);
}
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("Count", Counter);
savedInstanceState.putInt("Add", add);
}
public int ButtonCounter(int Counter) {
Counter += 1;
return Counter;
}
public int AutoCounter(int Counter, int add) {
Counter += add;
return Counter;
}
public void Display(int Counter, TextView myTextView) {
String man = String.valueOf(Counter);
myTextView.setText("$" + man);
}
}
you're not implementing the right method
from the docs:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt("Count", Counter);
savedInstanceState.putInt("Add", add);
// call superclass to save any view hierarchy
super.onSaveInstanceState(outState);
}
Source: https://developer.android.com/guide/components/activities/activity-lifecycle.html#oncreate
I'm working on android project. In my activity i have four buttons. The user has to select the particular button to receive the notifications. So the next time if i select Notifications(its a fragment of nav drawer) it should directly lead me to that particular activity instead of again selecting the department.
1st Run
Nav Drawer (select Notification)>>Opens Activity(to choose button to receive notification) >> Opens that particular activity
2nd Run
Nav Drawer(select Notification)>> Directly opens the selected activity
Notification.class
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.NotificationCompat;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.mateoj.multiactivitydrawer.department.dept_1styeartab;
import com.pushbots.push.Pushbots;
public class notifications extends BaseActivity {
private WebView webView;
Button clickButton;
Button clickButton1;
Button clickButton2;
Button clickButton3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Pushbots.sharedInstance().init(this);
Pushbots.sharedInstance().setCustomHandler(customHandler.class);
setContentView(R.layout.activity_notifications);
SharedPreferences preferences = getSharedPreferences("com.mateoj.multiactivitydrawer", MODE_PRIVATE);
final SharedPreferences.Editor editor = preferences.edit();
clickButton = (Button) findViewById(R.id.ncs);
clickButton1 = (Button) findViewById(R.id.nmech);
clickButton2 = (Button) findViewById(R.id.nece);
clickButton3 = (Button) findViewById(R.id.neee);
clickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent show = new Intent(getApplicationContext(), noti_cse.class);
Pushbots.sharedInstance().tag("cse");
Pushbots.sharedInstance().untag("mech");
Pushbots.sharedInstance().untag("ece");
Pushbots.sharedInstance().untag("eee");
editor.putString("session", "cse").commit();
editor.commit();
startActivity(show);
}
});
clickButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent show = new Intent(getApplicationContext(), noti_mech.class);
Pushbots.sharedInstance().tag("mech");
Pushbots.sharedInstance().untag("ece");
Pushbots.sharedInstance().untag("eee");
Pushbots.sharedInstance().untag("cse");
editor.putString("session", "mech").commit();
editor.commit();
startActivity(show);
}
});
clickButton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent show = new Intent(getApplicationContext(), noti_ece.class);
Pushbots.sharedInstance().tag("ece");
Pushbots.sharedInstance().untag("mech");
Pushbots.sharedInstance().untag("eee");
Pushbots.sharedInstance().untag("cse");
editor.putString("session", "ec").commit();
editor.commit();
startActivity(show);
}
});
clickButton3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent show = new Intent(getApplicationContext(), noti_eee.class);
Pushbots.sharedInstance().tag("eee");
Pushbots.sharedInstance().untag("ece");
Pushbots.sharedInstance().untag("mech");
Pushbots.sharedInstance().untag("cse");
editor.putString("session", "eee").commit();
editor.commit();
startActivity(show);
}
});
onStartUp();
}
#Override
protected void onDestroy() {
super.onDestroy();
onStartUp();
}
#Override
protected void onPause() {
super.onPause();
onStartUp();
}
#Override
protected void onStop() {
super.onStop();
onStartUp();
}
private void onStartUp()
{
SharedPreferences sharedPreferences = getSharedPreferences("com.mateoj.multiactivitydrawer", Context.MODE_PRIVATE);
String str = sharedPreferences.getString("session", "");
if (str.equals("cs")) {
clickButton.setClickable(true);
clickButton1.setClickable(false);
clickButton2.setClickable(false);
clickButton3.setClickable(false);
} else if (str.equals("mech")) {
clickButton.setClickable(false);
clickButton1.setClickable(true);
clickButton2.setClickable(false);
clickButton3.setClickable(false);
} else if (str.equals("ec")) {
clickButton.setClickable(false);
clickButton1.setClickable(false);
clickButton2.setClickable(true);
clickButton3.setClickable(false);
} else if (str.equals("eee"))
{
clickButton.setClickable(false);
clickButton1.setClickable(false);
clickButton2.setClickable(false);
clickButton3.setClickable(true);
}
}
/* private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
} */
#Override
protected boolean useDrawerToggle() {
return false;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_credits)
return true;
if (item.getItemId() == android.R.id.home)
onBackPressed();
return super.onOptionsItemSelected(item);
}
}
The onStartup code saves the preferences
I am having a screen which has a WebView showing a UI developed in HTML and PHP. Now, when i migrate to a different screen in Android and want to come back to my parent screen, I want to have a NEW WebView layout. Even though I am passing a String variable back for validating purposes, but still then it does not work out. Does this have to do anything with the savedInstanceState? Any suggestions would be appreciated. Below is the code snippet.
package com.example.notifboard;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class AdminLogin extends Activity {
boolean flag = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
flag = true;
}
protected void onResume() {
super.onResume();
if (flag == true) {
setContentView(R.layout.adminloginmixed);
final Button AdminBtn = (Button)findViewById(R.id.AdminBtn);
final Button EventGenBtn = (Button)findViewById(R.id.EventGenBtn);
final Button BackBtn = (Button)findViewById(R.id.BackBtn);
AdminBtn.setBackgroundColor(Color.WHITE);
AdminBtn.setTextColor(Color.BLACK);
EventGenBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
EventGenBtn.setBackgroundColor(Color.WHITE);
EventGenBtn.setTextColor(Color.BLACK);
AdminBtn.setBackgroundColor(Color.parseColor("#1D326B"));
AdminBtn.setTextColor(Color.WHITE);
BackBtn.setBackgroundColor(Color.parseColor("#1D326B"));
BackBtn.setTextColor(Color.WHITE);
Intent EveGenIntent = new Intent(AdminLogin.this, EventGen.class);
startActivity(EveGenIntent);
finish();
}
});
AdminBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
EventGenBtn.setBackgroundColor(Color.parseColor("#1D326B"));
EventGenBtn.setTextColor(Color.WHITE);
BackBtn.setBackgroundColor(Color.parseColor("#1D326B"));
BackBtn.setTextColor(Color.WHITE);
AdminBtn.setBackgroundColor(Color.WHITE);
AdminBtn.setTextColor(Color.BLACK);
}
});
BackBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
BackBtn.setBackgroundColor(Color.WHITE);
BackBtn.setTextColor(Color.BLACK);
EventGenBtn.setBackgroundColor(Color.parseColor("#1D326B"));
EventGenBtn.setTextColor(Color.WHITE);
AdminBtn.setBackgroundColor(Color.parseColor("#1D326B"));
AdminBtn.setTextColor(Color.WHITE);
Intent BackBtnIntent = new Intent(AdminLogin.this, SelectEntryType.class);
startActivity(BackBtnIntent);
finish();
}
});
WebView browser = (WebView)findViewById(R.id.Adminloginwebview);
browser.setWebViewClient(new WebViewClient());
browser.loadUrl("http://sivasphpmobileapps.site90.net/NotificationBoard/AdminLogin.php");
}
else {
setContentView(R.layout.adminloginmixed);
final Button AdminBtn = (Button)findViewById(R.id.AdminBtn);
final Button EventGenBtn = (Button)findViewById(R.id.EventGenBtn);
final Button BackBtn = (Button)findViewById(R.id.BackBtn);
AdminBtn.setBackgroundColor(Color.WHITE);
AdminBtn.setTextColor(Color.BLACK);
EventGenBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
EventGenBtn.setBackgroundColor(Color.WHITE);
EventGenBtn.setTextColor(Color.BLACK);
AdminBtn.setBackgroundColor(Color.parseColor("#1D326B"));
AdminBtn.setTextColor(Color.WHITE);
BackBtn.setBackgroundColor(Color.parseColor("#1D326B"));
BackBtn.setTextColor(Color.WHITE);
Intent EveGenIntent = new Intent(AdminLogin.this, EventGen.class);
startActivity(EveGenIntent);
}
});
AdminBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
EventGenBtn.setBackgroundColor(Color.parseColor("#1D326B"));
EventGenBtn.setTextColor(Color.WHITE);
BackBtn.setBackgroundColor(Color.parseColor("#1D326B"));
BackBtn.setTextColor(Color.WHITE);
AdminBtn.setBackgroundColor(Color.WHITE);
AdminBtn.setTextColor(Color.BLACK);
}
});
BackBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
BackBtn.setBackgroundColor(Color.WHITE);
BackBtn.setTextColor(Color.BLACK);
EventGenBtn.setBackgroundColor(Color.parseColor("#1D326B"));
EventGenBtn.setTextColor(Color.WHITE);
AdminBtn.setBackgroundColor(Color.parseColor("#1D326B"));
AdminBtn.setTextColor(Color.WHITE);
Intent BackBtnIntent = new Intent(AdminLogin.this, SelectEntryType.class);
startActivity(BackBtnIntent);
}
});
WebView browser = (WebView)findViewById(R.id.Adminloginwebview);
browser.setWebViewClient(new WebViewClient());
browser.loadUrl("http://sivasphpmobileapps.site90.net/NotificationBoard/NewEvents.php");
}
}
}