I am currently making an app that containing a widget.
When placing the app a WidgetConfigure Activity opens where you can select an object out of a recycleViewer.
The object (Kitten) implements the parcelable and the recyleviewer is filled by an adapter class.
When the eventListner of that recyleviewer is triggered, the selected object must be passed to the widget since I need properties from that object in my widget.
I'm currently completely stuck and confused how I can pass an object to the widget.
How can I pass an object to the widget?
Here is my code that will run in the eventListner in
LongEventConfigureActivity:
private void makeWidget(Event myObj){
// How I normally passing data to an activity/fragment
//Intent i = new Intent();
//Bundle b = new Bundle();
//b.putParcelable(CONST_PARCELKEY, myObj);
//i.putExtras(b);
//i.setClass(this, ObjectDetailActivity.class);
//startActivity(i);
// Example of how to pass data to the widget
final Context context = LongEventWidgetConfigureActivity.this;
// When the button is clicked, store the string locally
String widgetText = txtWidgetText.getText().toString();
saveTitlePref(context, mAppWidgetId, widgetText);
// It is the responsibility of the configuration activity to update the app widget
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
LongEventWidget.updateAppWidget(context, appWidgetManager, mAppWidgetId);
// Make sure we pass back the original appWidgetId
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
#Override
public void onItemClick(Event e) {
makeWidget(e);
}
Also I need to find a way when the user tapping this widget, a certain activity of my app will open that will receive that object.
Summery of the problem (I don't have the privilege to post an image yet)
Related
New to Android Studio. I'm creating an app project for practice and I am trying to create a Menu Activity. I want to test to see if I can mute sounds and hide the display of text (score) via a Menu UI. I get that I can use Intent to pass values back and forth between activities and that I can use those values to turn features on and off across the app.
I cannot figure out with a button and onClick how to get a variable to change so that I can pass it via Intent. I've only seen it done INSIDE the onClick. I'm trying to change the variable OUTSIDE the onClick.
Example Code:
public class Menu extends AppCompatActivity {
private boolean soundOn = true;
private Button isSoundOn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
isSoundOn = findViewById(R.id.isSoundOn_button);
isSoundOn.setOnClickListener(v -> {
soundOn = false;
});
Now when I attempt to access the soundOn parameter and pass it on via Intent to another activity the value remains true, it never changes after the button is clicked.
I did figure out one trick, I can use intent and pass the value to the same activity, like so:
soundOff.setOnClickListener(v -> {
Intent intent = new Intent(Menu.this, Menu.class);
intent.putExtra("soundOn", false);
startActivity(intent);
This reloads the Activity after the button is clicked, it makes it appear as though a glitch happened as it is run, but I think that issue could be resolved via altering the transition animation...I think. However, this seems like a clumsy approach, especially in a Menu Activity that could have numerous settings to edit (sound, score, language, timer, color, background, etc.).
I get that I can have the onClick go back to the original Activity with the change, but I want to create a menu where I can have multiple selections made and then pass them all back to the original Activity.
I hope this makes sense, I know this is rather basic, but I'm new to this and my searching hasn't been able to yield a solution. Thanks.
If you are doing an intent to the same Activity you should retreive your intent on the onCreate method:
isSoundOn = intent.getBooleanExtra("soundOn", true) //true is the default parameter in case intent does not contain that key;
That way you are always checking your intent in case you need it.
You also need to use your variable in the intent; right now you are always setting it to false:
soundOff.setOnClickListener(v -> {
Intent intent = new Intent(Menu.this, Menu.class);
intent.putExtra("soundOn", soundOn);
startActivity(intent);
}
There are other solutions, for example: you can use SharedPreferences for persisting your values and then call this.recreate to recreate Activity again and avoid using intents. Then you can retreive your SharedPreferencesvalues on the onCreate method to do whatever you want.
Now when I attempt to access the soundOn parameter and pass it on via
Intent to another activity the value remains true, it never changes
after the button is clicked.
Lets start with keeping track of soundOn
When Menu activity is first launched soundOn = true
When isSound button is clicked soundOn = false
Intent intent = new Intent(Menu.this, Menu.class);
intent.putExtra("soundOn", soundOn); // false is the value that is extra.
startActivity(intent);
When MenuActivity is again launched due to the intent soundOn = true this is because of this line
private boolean soundOn = true; //<---
You are passing Extra in intent but you arent storing the intents extra value in soundOn
Thats why it is true always
to solve this use need to Get the Intent Extra that you have passed and we do it in onCreate method
private boolean soundOn = true;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Intent intent = getIntent();
if(intent != null) { // we need to get this cause when we first start our app we dont start it with any intents
soundOn = intent.getExtra("soundOn");
}
}
We have an aspx page with a form in it and we can only process this form in a browser-based environment (ie we cannot create a web service for it.
What we want to do is that we want to process this information in an android application. We want to send some initial data to this page, let the user fill it, submit the form, and then return the results to the calling activity, so we can show a proper message and act accordingly.
let me give an example for this usecase:
We are in activity A and in this activity we have a button that triggers the start of our journey:
//we are in activity A
mOurButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putExtra("additional_data_we_need", adittionalData);
startActivityForResult(intent, REQUEST_CODE);
}
});
Now we are in activity B and here, we should comunicate with that webpage that I was talking about, passing it the additionalData that is in this activity's extra bundle (it is acceptable if we do it using query strings or any other way).
After the user fills the form, and hit submit,we do some operation on it and return some results to another (web)page or in the current (web)page's postback (again it is acceptable if we use query strings, post method or any other way; whicever way that works for this senario). We get this returned result in android side and then get the result to ActivityA :
//we are in acitivity B
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
int adittionalData = getIntent().getExtras().getIntExtra("adittional_data_we_nedd");
//this is the first place that I don't know what to do
callTheWebPageAndGiveItTheData(adittionalData);
}
public void SomeListenerOfTheReturnedDataFromTheWebpage(Bundle returnedData //doesn't necessarily need to be of bundle type) {
intent intent = new Intent();
intent.putExtra("return_bundle", returnedData);
setResult(Activity.RESULT_OK, intent);
finish();
}
What can I do to achieve this goal?
Thanks in advance for your kind answers
I wrote a collection widget to show calllogs in home screen.
So there may have 2 receivers exist: one is widget provider the other is call log content observer. Im not sure whether content observer is a receiver or not.
CallLogAppWidgetProvider is declared in manifest.xml and I need to store some data in this receiver object (for using later in observer).
CallLogContentObserver is constructed and registered when WidgetProvider triggered. Observer is triggered when calls are made.
The problem is, when I first place my widget, widgetprovide is constructed, triggered and onUpdate is called, I can see CallLogAppWidgetProvider.mContext and CallLogAppWidgetProvider.mWidgetIds is set in app's main thread(tid=8148,pid=8148).
but when calls are made, CallLogContentObserver.onChange is called in the same thread(tid=8148,pid=8148). Inside onChange, mContext has the value that set previously but mWidgetIds is null, so mywidget could not reflect calllog's changes.
According to this post: Save data in Broadcast receiver , my widgetprovider may be destroyed when it returned, so it could explain why mWidgetIds is null, but conflict with mContext stored old value. And that post did not explain why we need persist our data in receivers.
If widgetprovider is destroyed, then when observer triggered how did android start to run observer's onChange? Is it the same mechanism like closures in lua? Why mContext can store a value?
If widgetprovider is not destroyed, then why mWidgetIds is null when observer runs? I dont know exactly how java passes primitive array, according to this post: Is an array a primitive type or an object (or something else entirely)? , arrays are runtime classes created by jvm, so mWidgetIds is a strong reference, the integer array mWidgetIds points to can not be gc after provider returned, because at least mWidgetIds points to it. What happend when provider returned?
Could you please tell me what on earth happend behind thses scenes? Any information will be precitated, thank you.
code is below:
public class CallLogAppWidgetProvider extends AppWidgetProvider {
private static final String TAG = "CallLogAppWidgetProvider";
private ContentObserver mContentObserver = null;
private Handler mHandler = new Handler();
private Context mContext = null; //data to store
private int[] mWidgetIds; //data to store
protected class CallLogContentObserver extends ContentObserver {
public CallLogContentObserver(Handler handler) {
super(handler);
// TODO Auto-generated constructor stub
}
#Override
public void onChange(boolean selfChange) {
AppWidgetManager.getInstance(mContext).notifyAppWidgetViewDataChanged(mWidgetIds, R.id.calllog_list);
}
}
#Override
public void onEnabled(Context context) {
mContext = context; //store data when first place in home screen
mContentObserver = new CallLogContentObserver(mHandler);
context.getContentResolver().registerContentObserver(CallLog.CONTENT_URI, true, mContentObserver);
}
#Override
public void onDisabled(Context context) {
context.getContentResolver().unregisterContentObserver(mContentObserver);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
mWidgetIds = appWidgetIds; //store data when first place in home screen
for (int i = 0; i < appWidgetIds.length; i++) {
Intent intent = new Intent(context, CallLogRemoteViewsService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.calllog_appwidget);
rv.setRemoteAdapter(R.id.calllog_list, intent);
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
I am relatively new to the Eclipse & ADT plugin world, so any answers can you please explain what it's doing? It would be very helpful.
Basically, I have a list in one activity that will be populated by a HTTP request from a database API, this I am still working on. However, what I wish to know, is am I able to take the string in the ListItem and give that to a TextView in the next activity?
So for example, I tap 'Record 1' and it takes 'Record 1' and puts it inside a variable, then sends the user to the next screen, and inserts the variable into a TextView. Is this possible?
Thank you.
Get what you want from the adapter, then put that into the Intent that starts the new activity:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String string = parent.getAdapter().getItem(position);
Intent intent = new Intent(this, nextActivity.class);
intent.putExtra("text", string);
startActivity(intent);
}
});
In the new Activity you then get the Intent that started the new Activity and get the String you put into it:
String data = getIntent().getStringExtra("text");
Implement the setOnItemClickListener() for the listItem where you will capture the listItem clicked and the String you require and then using intent.putExtra(String id, STRING_VALUE) you can pass your string to the next activity.
Yes, it is possible.
You can send information to another activity using Extra. See this code below:
Intent i = new Intent(this, NoteEdit.class);
i.putExtra(NotesDbAdapter.KEY_ROWID, id);
startActivityForResult(i, ACTIVITY_EDIT);
You use putExtra to put your data do you want to send to the activity. In the example NotesDbAdapter.KEY_ROWID is the name you give to access the data and id is the data itself.
And, this is how you can get the data from the activity:
Bundle extras = getIntent().getExtras();
mRowId = (extras == null) ? null : extras.getLong(NotesDbAdapter.KEY_ROWID);
I have the following code:
chart.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final String aux= (String) lt.getItemAtPosition(position);
Intent myIntent = new Intent(infoList.this, tabList.class);
startActivity(myIntent);
}
});
I also have a ListView. When I click on an item from that ListView I navigate to another activity that shows me the info for that activity. How do I do that? I want that info to correspond to the item I clicked.
Here is what I did in this situation, if you don't want to just pass an id back:
I call the other activity with this:
Intent intent = new Intent(myapp, CreateExerciseActivity.class);
intent.putExtra("selected_id", workout.getId());
startActivityForResult(intent, CREATE_ACTIVITY);
I then do
Intent returnIntent = new Intent();
returnIntent.putExtra("name", m.getName());
returnIntent.putExtra("unit", m.getUnit());
returnIntent.putExtra("quantity", m.getQuantity());
if (getParent() == null) {
setResult(Activity.RESULT_OK, returnIntent);
} else {
getParent().setResult(Activity.RESULT_OK, returnIntent);
}
finish();
So, in this case I was passing in an id in order to get more details from the user, and then I pass those back to the calling activity, in order to add it, as I didn't want to save this until the user chooses to later.
So, you need to do startActivityForResult in order to have it able to return the data.
You can add some data to the Intent with the methods putExtra(), and then retrieve the data in the new Activity with getExtras().getSomething().
i guess you have to use OnItemClickListener instead of the click event and you need intent to call the next activity.
private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id)
{
Intent intent = new Intent(this, MyInfoActivity.class);
intent.putExtra("selected_id", getIdFor(position));
startActivity(intent);
}
};
mHistoryView = (ListView)findViewById(R.id.history);
mHistoryView.setOnItemClickListener(mMessageClickedHandler);
Possible answer: How do I pass data between Activities in Android application?
http://thedevelopersinfo.wordpress.com/2009/10/15/passing-data-between-activities-in-android/
It really depends on what the data is in your listview. For example, if you're displaying a list of contacts in the listview, you could just pass the ID of the contact over to the other activity, and let that activity access the content provider for contacts to retrieve the data you want it to work with. You'd pass an ID within a URI in the data field of the intent, as opposed to in the extras bundle.