How to put the correct intent parameters? - java

I tried to make a SceneView intent where a 3d-model can be viewed.
Intent sceneViewerIntent = new Intent(Intent.ACTION_VIEW);
sceneViewerIntent.setData(Uri.parse("https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Avocado/glTF/Avocado.gltf"));
sceneViewerIntent.setPackage("com.google.android.googlequicksearchbox");
sceneViewerIntent.putExtra("mode","3d_only");
mContext.startActivity(sceneViewerIntent);
When this intent opens, it gives the user an option to view the model in your own environment, but I want to turn this off and according to https://developers.google.com/ar/develop/java/scene-viewer#3d-or-ar you should be able to pass on a parameter called 'mode' which can have the value '3d_only' which should prevent the user from being able to view the model in AR.
I tried to pass on this value by means of sceneViewerIntent.putExtra("mode","3d_only"); but it doesn't work. Am I passing on the information correctly?

You're not sending any information anywhere. "3d_only" should be some variable, otherwise you're not sending anything. Also, you have to retrieve your code on the other end by doing String string = getIntent().getStringExtra("mode");

Related

Which is considered better to pass information between Activities on Android project, send a model or only an id?

I have an android app and I have 3 screens that the user must fill some fields. Each screen have 7 fields to be filled. At the end of the process, I submit the information to the server using Retrofit with an API.
This is how I pass the information between Activities in my first project
// first activity
AutoDados autuacaoDados = new AutoDados(... a lot of parameters ...);
Intent intent1 = new Intent(AutoInf_Lista_Generica.this, AutoInf_Menu.class);
intent1.putExtra("PARAM_ENV_DADOS", autuacaoDados);
startActivity(intent1);
// second activity
Bundle extras = intent.getExtras();
if (extras == null) {
return "";
}
AutoDados autuacaoDados = (AutoDados) extras.getSerializable("PARAM_ENV_DADOS");
I have another project that I use the following code to pass data between activities.
// first activity
Intent intent1 = new Intent(AutoInf_Lista_Generica.this, AutoInf_Menu.class);
intent1.putExtra("ID_AUTOS", myId);
startActivity(intent1);
// second activity
intent.getIntExtra("ID_AUTOS", 0);
// code that recover the data from a SQLite database based in the "ID_AUTOS"
These both ways I was able to recover data between activities, but which way is considered better and why is it better?
Another question, is there a better way to pass data between activities ?
If possible, talk about the pros and cons about each approach.
The main difference between the tow ways is that in the first method you are passing an Object to the second activity while in the second method you are passing a primitive type.
which is better ? each way has its own use case, there is no better way, it depends on your use case

How can I share array lists between activities in android studio?

I am trying to build an android app and I have a problem. First of all, I wrote a method which access your phone memory and imports an excel file which contains two columns with values that represents time and heart rate. I read the data from excel and stored it into an array list and this array list it is
DataFitbit type (DataFitbit is a class I made which contains two parameters, like excel file, time and heart rate). Now I want to create a chart with this data in other activity, but I think the code wrote in order to share my arraylist between those two activities doesn't work. I don't have any error, but when I push the button that create another activity where I want to create the chart, the app crashes.
This is the code wrote in order to share the array list between activities:
Bundle bundle = getIntent().getExtras();
ArrayList<DateFitbit> dateFitbit = (ArrayList<DateFitbit>) bundle.get("arraylist");
ListView listView = findViewById(R.id.listView);
ArrayAdapter<DateFitbit> items = new ArrayAdapter<DateFitbit>(this,android.R.layout.simple_list_item_1);
listView.setAdapter(items);
The code that opens a new activity which will contain charts is:
public void openChartActivity(){
Intent intent = new Intent(this, ChartActivity.class);
intent.putExtra("arraylist",dateFitbit);
startActivity(intent);
}
In the very first time I tried to see my data into a list view, to be sure that the share has been done, but as I said, something doesn't work well.
Do you have any suggestions for me?
You're not passing a basic String ArrayList, instead you're using a custom data type object for ArrayList.
For that to work, you need to do something as mentioned here:
1. Now, pass it in intent as:
Bundle bundle = new Bundle();
bundle.putSerializable("arraylist",(Serializable) dateFitbit);
intent.putExtra("Bundle",bundle);
startActivity(intent);
2. In the receiving activity, get it as:
Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("Bundle");
ArrayList<DataFitBit> dateFitbit= (ArrayList<DataFitBit>) bundle.getSerializable("arraylist");
This will preserve your custom ArrayList model and you can then utilize it as you prefer.

how can forward data form i activity to 3 activity via 2 activity

I am making my own project but I am stuck.
Getting the information from the user in the first activity is working just fine.
But when I try to get those numbers, ints, in the third activity via a second activity, it is showing me the default value.
I am trying it using an Intent, but it is not working.
Intent intent = new Intent(this, active.class);
Intent intent2 = new Intent(this, BMR_Active.class);
intent.putExtra(EXTRA_TEXT_height,height );
intent.putExtra(EXTRA_TEXT_weight,weight);
intent.putExtra(EXTRA_TEXT_age,age);
startActivity(intent);
It is giving me a default value i.e. 0(I have given) in BMR activity then i am going via active activity.
Your putExtra method via intent would work fine, but are you also running putExtra BEFORE starting the third activiy?
The data is first passed from first activity to second activity, then from second activity is passed to third activity using the same method.
Additional Note:
In cases like this, I would usually use fragments instead, I would store the data in the activity and fragments will obtain the datas via getActivity(). This will eliminate the needs of multiple passing of data around.
Based on what you said in your comment about passing data straight from Activity 1 to Activity 3, then what you can do is the following:
Page1.class:
Intent toPage3 = new Intent(Page1.this, Page3.class);
int myNum = 5;
toPage3.putExtra("Number", myNum);
startActivity(toPage3);
Page3.class:
Intent receiveIntent = getIntent();
int numFromPage1 = receiveIntent.getIntExtra("Number");
I believe that this should answer your question. numFromPage1 will be an int containing the value from Page1.

android widgets and persistent data

I have a litte appwidget with a configuration activity.
In the configuration activity I have a EditText
to store a particular string and a button to create the widget.
In the widget itself there is only a Imageview with a static image showing a button.
when i press the button im doing the following:
AppWidgetManager app_widget_manager = AppWidgetManager.getInstance(context);
RemoteViews local = new RemoteViews(context.getPackageName(), R.layout.main);
app_widget_manager.updateAppWidget(widget_id, local);
Intent create = new Intent(this, MyWidget.class);
create.setAction("com.test.mywidget.CREATE");
create.putExtra("toastmessage", edittext1.getText().toString);
context.sendBroadcast(create);
Intent result_value = new Intent();
result_value.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widget_id);
setResult(RESULT_OK, result_value);
finish();
the extra 'toastmessage' is just the content of the edittext.
in the widget i catch the broadcast in onReceive() and get the string value that was
passed before as extra 'toastmessage' to the new widget.
if i press on my widget, i create a toast with the passed string
- and here is my problem -
the passed string is different for every widget created.
but the variable (in the widget) where i store the passed string
gets resettet after a little time or when the phone gets restartet or the app killed or something similar,
so i need persistent data for every widget instance.
I need to store which widget got which string passed,
and get this data everytime when the widget gets updated.
How can i achieve this?
According with this link you have some options to accomplish your goal.
For shared preferences, here an example.
For internal storage Take this way.
For external storage, you can use this.
For SQL Lite storage, here.
Hope these examples helps, cheers.
The widget_id you received is a unique identifier of the widget instance. Use it as a key to identify your persisted data.

Intent putExtra(String,String) inside of AsyncTask

I'm simply trying to carry a string onto the next activity without having to define an entire object for the task. I've seen similar solutions and gotten them to work BUT without using AsyncTask to create the intent.
protected void onPostExecute(Boolean result) {
if (loggedIn && hasPin) {
Intent intent = new Intent(UniteActivity.this,
WebViewActivity.class);
intent.putExtra(PASSED_USERNAME, passUser);
startActivity(intent);
}
if (loggedIn && !hasPin) {
Intent intent = new Intent(UniteActivity.this,
CreatePinActivity.class);
intent.putExtra(PASSED_USERNAME, passUser);
startActivity(intent);
PASSED_USERNAME is a public static constant to hold the package name, just as the putExtra() method requires. I then try to pull the value out in the next activity.
Intent extras = getIntent();
String username = extras.getStringExtra(UniteActivity.PASSED_USERNAME);
// carry username to next activity
Intent intent = new Intent(CreatePinActivity.this,WebViewActivity.class);
intent.putExtra(PASSED_USERNAME, username);
startActivity(intent);
There is never a String to pull out, the value of username is always null. I've gone through the debugger and found that the Eclipse IDE debugger shows different intent ID's between the activities, they are never consistant. Is it possible that AsyncTask is interfereing somehow because it splits into a seperate thread?
I don't know if this applies to your problem, because I can't see from your code snippet if the intermediary activity is freshly created or not.
BUT: the getIntent()-method always returns the first Intent that started the activity. If the activity remains in the background and receives a new Intent this value does not get updated automatically. You have to override onNewIntent(...) and manually call setIntent(...) for this to work (or do all your stuff directly there).
So just for the case that you do not run your posted code in the onCreate() method please check if you did not miss to fetch the real intent you are interested in.
Not sure of the exact answer for you solution.
You calling startActivity on the UI since it's in postExecute().
If all else fails you can just save that value to a sharedpreference.
The way you have handled the variable PASSED_USERNAME seems incorrect. You have used it in some palaces as simple PASSED_USERNAME whereas in some other places you have used it with the class named prefixed UniteActivity.PASSED_USERNAME. Since it is a Public Static Constant always use it prefixed with the class name.

Categories

Resources