I have a simple program that changes the background of activity A from activity B.
When you change the background you need to refresh activity A in order for the background to change, after looking around stackoverflow the easiest way was just to call recreate().
I'm not sure if im calling it wrong or in the wrong area but what ends up happening is it will loop the following error when the app is run and eventually crash-
02-01 13:23:53.358 17302-17302/com.package.www.randomapp E/ViewRootImpl: sendUserActionEvent() mView == null
Here's the code for activity A
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainmenu);
backgroundChanger();
recreate();
}
public void backgroundChanger(){
SharedPreferences sharedGradients = getSharedPreferences("gradientInfo", Context.MODE_PRIVATE);
int backgroundGrad = sharedGradients.getInt("backgroundGradient", 0);
if (backgroundGrad == 0){
MMBackground.setBackgroundResource(R.drawable.blackgreengradiant);
}
if (backgroundGrad == 1){
MMBackground.setBackgroundResource(R.drawable.blueblackgradiant);
}
if (backgroundGrad == 2){
MMBackground.setBackgroundResource(R.drawable.goldblackgradiant);
}
and for Activity B
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_options_menu);
variableHandler();
}
public void variableHandler() {
MainMenuBackgroundBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
final SharedPreferences[] sharedGradients = {getSharedPreferences("gradientInfo", Context.MODE_PRIVATE)};
final SharedPreferences.Editor[] editor1 = {sharedGradients[0].edit()};
final SharedPreferences[] sharedBoolean = {getSharedPreferences("binaryPoint", Context.MODE_PRIVATE)};
final SharedPreferences.Editor[] editorBinary = {sharedBoolean[0].edit()};
final PopupMenu popup = new PopupMenu(getApplicationContext(), v);
popup.inflate(R.menu.menu_background_gradiant_setter);
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(OptionsMenu.this, "Blue and black gradient", Toast.LENGTH_SHORT).show();
editorBinary[0] = sharedBoolean[0].edit();
editorBinary[0].putInt("binaryPoint", 1);
editor1[0] = sharedGradients[0].edit();
editor1[0].putInt("backgroundGradient", 1);
editor1[0].apply();
return true;
case R.id.item2:
Toast.makeText(OptionsMenu.this, "Gold and black gradient", Toast.LENGTH_SHORT).show();
editorBinary[0] = sharedBoolean[0].edit();
editorBinary[0].putInt("binaryPoint", 1);
editor1[0] = sharedGradients[0].edit();
editor1[0].putInt("backgroundGradient", 2);
editor1[0].apply();
return true;
}
}
The problem is that you are calling recreate() in the onCreate() method of the Activity without any condition which will create an infinite loop. Keep a variable to track whether the activity is recreated or not.
private static boolean alreadyRecreated = false;
//You can add some extra conditions here if you want.
if(!alreadyRecreated){
recreate();
alreadyRecreated = true;
}
recreate(); will cause your activity to be recreated.
i.e, onCreate gets called. Since, you added recreate(); in onCreate method, it is running into infinite loop and crashing.
Related
I'm working on a simple log app that lets the user enter a time and a note, and then displays the entered data in a ListView in a dedicated activity (MainActivty). The time and data are entered in a separate activity (AddTimeActivity) with two EditText's and are passed to MainActivity when tapping the save button through an adapter (TimeTrackerAdapter). Alternatively, a cancel button can be pressed when the user changes their mind. The AddTimeActivity can be accessed through an add button in the action bar default menu. Now I've added a delete button -which is working fine- and an edit button to each row in the list. Now The problem is: How can I add the editing feature without making a new activity dedicated to editing. In Other words, how can I make the AddTimeActivity work with editing and adding in the same time, how can I make my app know that the user tapped the add button and start the AddTimeActivity with empty EditText's, or the user tapped the edit button in one of the rows in the list and passes the the data to be edited to AddTimeActivity and displays them in the EditText's and saves the edited data in the same entry? Sorry for not showing any attempts but I'm actually clueless about the issue.
MainActivity.java
public class MainActivity extends AppCompatActivity {
public TimeTrackerAdapter timeTrackerAdapter;
public int TIME_ENTRY_REQUEST_CODE = 1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.time_list);
timeTrackerAdapter = new TimeTrackerAdapter();
listView.setAdapter(timeTrackerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_main, menu);
return true;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TIME_ENTRY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
String time = bundle.getString("time");
String note = bundle.getString("note");
TimeRecord timeRecord = new TimeRecord(time, note);
timeTrackerAdapter.addTimeRecord(timeRecord);
timeTrackerAdapter.notifyDataSetChanged();
}
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
// noinspection SimplifiableIfStatement
if (id == R.id.add_time_item) {
Intent intent = new Intent(getApplicationContext(), AddTimeActivity.class);
startActivityForResult(intent, TIME_ENTRY_REQUEST_CODE);
return true;
}
else if (id == R.id.about) {
Intent aboutIntent = new Intent(getApplicationContext(), AboutScreen.class);
startActivity(aboutIntent);
}
return super.onOptionsItemSelected(item);
}
}
The AddTimeActivity, onSave and onCancel are the buttons' methods:
public class AddTimeActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_time);
}
public void onCancel(View view) {
finish();
}
public void onSave(View view) {
Intent intent = new Intent(AddTimeActivity.this, MainActivity.class);
EditText timeEditText = (EditText) findViewById(R.id.Time_Edit_Text);
String time = timeEditText.getText().toString();
EditText noteEditText = (EditText) findViewById(R.id.Note_Edit_Text);
String note = noteEditText.getText().toString();
intent.putExtra("time", time);
intent.putExtra("note", note);
this.setResult(RESULT_OK, intent);
finish();
}
}
TimeTrackerAdapter.java:
public class TimeTrackerAdapter extends BaseAdapter {
public ArrayList<TimeRecord> times = new ArrayList<TimeRecord>();
#Override
public int getCount() {
return times.size();
}
public TimeTrackerAdapter() {
times.add(new TimeRecord("12:30", "this is the best"));
times.add(new TimeRecord("2:30", "I need this"));
}
#Override
public Object getItem(int position) {
return times.get(position);
}
public void addTimeRecord(TimeRecord timeRecord) {
times.add(timeRecord);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View view, ViewGroup parent) {
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.menu_layout, parent, false);
}
TextView timeView = (TextView) view.findViewById(R.id.time_textView);
TextView noteView = (TextView) view.findViewById(R.id.note_TextView);
Button deleteButton = (Button) view.findViewById(R.id.delete_entry);
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
times.remove(position);
notifyDataSetChanged();
}
});
TimeRecord time = times.get(position);
timeView.setText(time.getTime());
noteView.setText(time.getNote());
return view;
}
}
The question is
How can I add the editing feature without making a new activity dedicated to editing.
or, more generally,
"how do I hand over information to the activity I'm calling ?"
You achieve this by adding extras to the intent which you use to start the activity. For example, in your 'MainActivity' before calling 'startActivityForResult()':
Intent intent = new Intent(this, MyOtherActivity.class);
// in your case, 'extraInformation' could be a boolean (add = yes|no)
intent.putExtra("MyExtraInformationKey", extraInformation);
startActivityForResult(intent,TIME_ENTRY_REQUEST_CODE);
Then in the 'onCreate()' method of the other activity, you question the intent for extras:
Intent i = getIntent();
if (i != null && i.hasExtra(getString("MyExtraInformationKey"))
{
boolean myInfo = i.getBooleanExtra("MyExtraInformationKey");
// proceed as appropriate...
}
For your case of buttons inside ListView rows, you could make the OnClickListener method call another method (like 'doCallMayOtherActivity()') in your 'MainActivity', handing over all relevant information (like the position in the 'times' ArrayList).
This method would then start your add/edit activity, passing the old data out of 'times.get(position)' in a bundle as an extra to the intent.
To access the methods in 'MainActivity' from your adapter class, you could use the following code in 'getView()'
Button editButton=(Button) view.findViewById(R.id.edit_entry);
editButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((MainActivity)parent.getContext()).doCallMyOtherActivity(times.get(position));
}
This way, your other activity could check for the existence of that bundle. If there is none, you have a case of 'add'. Else, you have a case of 'edit'.
Im having issues with the app crashing with nullpoint exception.
I know that it crashes when trying to get an ArrayList from pictureTalkFragment. which in this class is only set to PictureTalkFragment ptf;
In other words im trying to get an element (have both getter/setter for the arraylist in ptf, and made the arraylist public as an alternative) from an class and not the instance of that class.
But im just to noob to figure out how to correctly handle getting the instances between classes (activity ---> fragments and back etc). In Java i usually just had an referance in the Constructor that sent the instance/referance with the creation of the new class. But in Android theres all this onCreate (getActivity,getContext ++), Im confused:P When to user where and how:(
the EditPicture was started from this code in GridViewAdapter that extended from PictureTalkFragment (edit in onlongclicklistener)
row.setOnLongClickListener(new View.OnLongClickListener()
{
#Override
public boolean onLongClick(View v) {
PopupMenu popMenu = new PopupMenu(v.getContext(), v);
popMenu.getMenuInflater().inflate(R.menu.picturetalk_popup_menu, popMenu.getMenu());
popMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.edit:
Intent intent = new Intent(getContext(), EditPicture.class);
intent.putExtra("itemUUID", item.getId());
String s = new String("");
context.startActivity(intent);
break;
case R.id.remove:
FileInteraction fileInteraction = new FileInteraction();
fileInteraction.deleteFilesAndFolder(item.getImagePath());
item.setTitle("");
notifyDataSetChanged();
break;
default:
//
}
return true;
}
});
popMenu.show();
return true;
}
});
return row;
EditPicture class
public class EditPicture extends Activity {
private EditText text;
private Button applyBtn;
private ArrayList<PictureItem> piArray;
private PictureItem pi;
private UUID itemID;
private PictureTalkFragment ptf;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
itemID = (UUID) getIntent().getSerializableExtra("itemUUID");
SetLocalArray(ptf.getArray()); //Nullpoint here, and i know why. But not how to get the allready created instance of this class
getPictureItem();
setContentView(R.layout.picturetalk_edit_pic);
text = (EditText) findViewById(R.id.editName);
text.setText(pi.getTitle());
applyBtn = (Button) findViewById(R.id.applyChangeBtn);
applyBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
updatePictureItem();
ptf.setArray(piArray);
}
});
}
private void updatePictureItem() {
pi.setTitle(text.toString());
piArray.add(pi);
ptf.setArray(piArray);
}
private void SetLocalArray(ArrayList<PictureItem> array) {
this.piArray = array;
}
private PictureItem getPictureItem() {
pi = new PictureItem("", "");
for (int i = 0; i < piArray.size(); i++) {
if (itemID.equals(piArray.get(i))) {
pi = piArray.get(i);
piArray.remove(i);
}
}
return pi;
}}
I don't know what you are using the array for.
Usually you should not depend on the fragment to get the info, if you want to pass an array of objects to the activity, you should use the Bundle in the activity extras to do so, instead of passing only the UUID, just pass also the array you need.
If you want the lazy option just make a class with a static variable to store the fragment and use it in the activity, which I don't advise.
I have two activities. In the second activity I have a spinner. what I would like to happen is after the user selects an item from the spinner it will save via actionbar press and on back press which will load the previous activity. Based on my research my activity is supposed to look something like the following below but it's not working what am I doing wrong??
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
getActionBar().setDisplayHomeAsUpEnabled(true);
spin = (Spinner)findViewById(R.id.editspin);
Intent i = this.getIntent();
note = new ArgueItem();
note.setKey(i.getStringExtra("key"));
note.setText(i.getStringExtra("text"));
EditText et = (EditText)findViewById(R.id.argueEdit);
et.setText(note.getText());
et.setSelection(note.getText().length());
}private boolean saveState() {
prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor prefEditor = prefs.edit();
int daddy = spin.getSelectedItemPosition();
prefEditor.putInt("savedValue",daddy);
prefEditor.commit();
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
EditText et = (EditText)findViewById(R.id.argueEdit);
String argueText = et.getText().toString();
if(argueText.equals("")){
Toast.makeText(this, "Please Enter A New ", Toast.LENGTH_SHORT).show();
return false;
}
if (item.getItemId() == android.R.id.home) {
saveAndFinish();
}
return false;
}
#Override
public void onBackPressed() {
EditText et = (EditText)findViewById(R.id.argueEdit);
String argueText = et.getText().toString();
if(argueText.equals("")){
Toast.makeText(this, "Please Enter A New ", Toast.LENGTH_SHORT).show();
return;
}else{
saveAndFinish();
}
In your second activity, you have to override the onPause() and. Inside it write the saving process.
protected void onPause(){
super.onPause();
//Include the code which, save the data.
}
You should use a FragmentActivity and add/remove fragments within the same activity.
check these resources:
http://developer.android.com/guide/components/fragments.html
http://www.vogella.com/articles/AndroidFragments/article.html
This is how i'm initializing my spinner which is in the ActionBar. I'm not adding it as a custom view, but I'm using the drop down menu feature.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setListNavigationCallbacks(adapter, new ActionBar.OnNavigationListener() {
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
//save in preferences
PreferenceManager.getDefaultSharedPreferences(MainActivity.this).edit().
putInt(SELECTED_DIARY_PREF, itemPosition).commit();
return true;
}
});
int selPosition = PreferenceManager.getDefaultSharedPreferences(this).getInt(SELECTED_DIARY_PREF, 0);
actionBar.setSelectedNavigationItem(selPosition);
What this code does is: saving the preference when an item of the menu is clicked, and restoring that preference when the activity is launched. Hope it helps.
I am just a starter in Android. I have an Android code which has a Button. On click of the button, it should Invoke AirPlane mode and then again back to normal mode. Here is my code :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// load controls
tvStatus = (TextView)findViewById(R.id.tvStatus);
togState = (Button)findViewById(R.id.togState);
// set click event for button
togState.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// check current state first
boolean state = isAirplaneMode();
// toggle the state
toggleAirplaneMode(state);
state = isAirplaneMode();
// toggle the state
toggleAirplaneMode(state);
}
});
}
public void toggleAirplaneMode(boolean state) {
// toggle airplane mode
Settings.System.putInt(this.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, state ? 0 : 1);
// broadcast an intent to inform
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !state);
sendBroadcast(intent);
}
public boolean isAirplaneMode() {
return Settings.System.getInt(this.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1;
}
}
The problem here is, my phone will go in AirPlane mode and it toggles back also. But this process I cannot stop. Is the problem with the way I handled the OnClick Listener by calling same method (toggleAirplaneMode) twice?
Regards,
This answer contains code necessary to do this. Also make sure you have the WRITE_SETTINGS permission.
Adapted from Controlling Airplane Mode:
// read the airplane mode setting
boolean isEnabled = Settings.System.getInt(
getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) == 1;
// toggle airplane mode
Settings.System.putInt(
getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);
// Post an intent to reload
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isEnabled);
sendBroadcast(intent);
Replace the onClick method with this:
public void onClick(View v) {
// check current state first
boolean state = isAirplaneMode();
// toggle the state
final Handler handler = new Handler(){
#Override
public void handleMessage(Message msg) {
toggleAirplaneMode(!state);
super.handleMessage(msg);
}
};
Thread th = new Thread() {
#Override
public void run() {
toggleAirplaneMode(!state);
handler.sendEmptyMessage(0);
};
};
th.start();
}
Every time you will click the button, it will toggle the airplaneMode.
If it doesn't work, try removing !
Check this out... This might help..
public class MainActivity extends Activity {
Context context;
private void changeRadioComponentEnabled(Context paramContext, String paramString, boolean paramBoolean1, boolean paramBoolean2)
{
boolean bool = false;
ContentResolver localContentResolver = paramContext.getContentResolver();
int i;
if (!paramBoolean1)
i = 1;
else
i = 0;
Settings.System.putInt(localContentResolver, "airplane_mode_on", i);
Settings.System.putString(paramContext.getContentResolver(), "airplane_mode_radios", paramString);
Intent localIntent = new Intent("android.intent.action.AIRPLANE_MODE");
if (!paramBoolean1)
bool = true;
localIntent.putExtra("state", bool);
paramContext.sendBroadcast(localIntent);
if (!paramBoolean2)
{
if (paramString.indexOf("cell") == 0)
Settings.System.putString(paramContext.getContentResolver(), "airplane_mode_radios", "cell");
}
else
Settings.System.putString(paramContext.getContentResolver(), "airplane_mode_radios", "cell,bluetooth,wifi,nfc");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = this;
((Button)findViewById(R.id.button1)).setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramAnonymousView)
{
MainActivity.this.changeRadioComponentEnabled(MainActivity.this.context, "cell", false, false);
}
});
((Button)findViewById(R.id.button2)).setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramAnonymousView)
{
MainActivity.this.changeRadioComponentEnabled(MainActivity.this.context, "cell", true, false);
}
});
}
I got it finally
I used this in my code
public void onClick(View v) {
// check current state first
boolean state = isAirplaneMode();
// toggle the state
toggleAirplaneMode(state);
state = isAirplaneMode();
// toggle the state
toggleAirplaneMode(state);
ser = new ServiceState();
ser.setState(STATE_IN_SERVICE);
}
And I have declared final int STATE_IN_SERVICE = 0; before OnCreate. And ser is the instance of ServiceState.
Thank you for your replies.
I have a EditText in a preference menu that allows me to edit a URL address. The problem is when I get the preference value in the mainActivity is not getting updated right away after I click OK in the Preference Menu. Not sure how to fix this problem. I tried a bunch of ideas and finally decided to ask.
public class PreferencesActivityTest extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.radio_preferences);
PreferenceManager.setDefaultValues(PreferencesActivityTest.this,
R.xml.radio_preferences, false);
EditTextPreference editPref =(EditTextPreference)findPreference("MyText");
editPref.setOnPreferenceChangeListener(
new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
if (newValue.toString().length() > 0) {
return true;
}
// If now create a message to the user
Toast.makeText(PreferencesActivityTest.this,
"Invalid Input", Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
PS: This code updates the newValue to what I enter in the EditTextPreference, doesn't carry the new value to the MainActivity until I modify it again...
UPDATE:
In OnResume() I can see that the value is updated with the one that I modified in the PreferenceActivityTest from EditTextPreference. What I'm trying to do is to pass this newValue into the SetDataSource("").
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_radio);
initializeMediaPlayer();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_edit:
editURL();
// make a Dialog or show an Activity
return true;
}
}
private void initializeMediaPlayer() {
PreferenceManager.setDefaultValues(this, R.xml.radio_preferences, false);
SharedPreferences pref =PreferenceManager.getDefaultSharedPreferences(this);
String radioPath = pref.getString("MyText", "default value");
// Toast.makeText(this, radioPath, Toast.LENGTH_SHORT).show();
try {
radioPlayer.reset();
// radioPlayer.setDataSource("http://31.xx.xxx");
// Toast.makeText(this, radioPath, Toast.LENGTH_SHORT).show();
radioPlayer.setDataSource(radioPath);
} catch {
}
}
public void editURL() {
stopPlaying();
startActivity(new Intent(getBaseContext(), PreferencesActivityTest.class));
}
I am doing something fundamentally wrong but I need help. Thank you in advance !
how are you calling the preference activity? If you are calling it directly, you probably need to change the call to startActivityForResult so you refresh your data once you return from the Activity