Moving an Adapter Object from one activity to another - java

I want to move an adapter object from one activity to another.
My main activity:
Intent intent = new Intent(MainActivity.this, ActivityTest.class);
intent.putExtra("Key1", position);
intent.putExtra("key2", toyAdapter);
startActivity(intent);
My second activity:
Intent intent = getIntent();
int position = intent.getIntExtra("Key1", 0);
ToyAdapter toyAdapter = (ToyAdapter) intent.getSerializableExtra("Key2");
I know something's wrong but I am pretty new to this and therefore I am not sure what's the right way to move that Adapter Object to my second activity. Thanks

Related

ActivityResultLauncher for 3 activities

I have 3 activities.
The first activity is a map (using Google Map API). In this activity, I also have a button that will direct to second activity.
In Second activity, it's just for additional information but there is a button. When this button is clicked, it will be directed to third activity.
In third activity, there's only a button that will bring a result to first activity (maps).
This is how I assigned my first activity's button:
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
And I also used ActivityResultLauncher in first activity like so:
ActivityResultLauncher<Intent> intentLaunch = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if(result.getResultCode() == Activity.RESULT_OK){
String data = result.getData().getStringExtra("SIMPAN");
Log.d(TAG, "berhasil");
}
}
);
In my third activity's button, I assigned it like this:
third_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.putExtra("SIMPAN", "Simpan");
setResult(Activity.RESULT_OK, intent);
finish();
startActivity(new Intent(ThirdActivity.this, FirstActivity.class)); // When the button is clicked, it will get back to first activity and bringing the result
}
});
But, turns out the first activity didn't get the result. I assume that I missing this particular code after using ActivityResultLauncher:
Intent intent = new Intent(FirstActivity.this, ThirdActivity.class);
intentLaunch.launch(intent);
However, it makes the application starts from third activity instead of first activity. It does receive the result (the log was showing) but it's only one time. How can I resolve this?
(Sorry for my bad english)
This is an overkill.
Simply:
Intent intent = getIntent();
String simpan = intent.getStringExtra("SIMPAN");
in your onCreate class
registerForActivityResult is used when you get a result from outside the app, such as getting a picture from the camera.

Show text on other activity than the button clicked

I know how to show text with a button click on the same page, but my question is (since I couldn't find anything on Google): Is it possible when you click a button, that the text shows up on another activity?
Yes, you can
In your FirstActivity execute this when button is clicked:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("data","Messsage to be sent");
startActivity(intent);
In your SecondActivity inside onCreate():
String someData = getIntent().getStringExtra("data");
yourTextView.setText(someData);
If you need pass values between the activities you use this:
String name="aaaa";
Intent intent=new Intent(Main_Activity.this,Other_Activity.class);
intent.putExtra("name", name);
startActivity(intent);
And this code to recovery data on new Activity:
Bundle b = new Bundle();
b = getIntent().getExtras();
String name = b.getString("name");
You can use Intent for this. Intent is used to move on other activity from first activity.
You can use :
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("YourValueKey", yourData.getText().toString());
then you can get it from your second activity by :
Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("YourValueKey");

Moving of image between ImageButtons in seperate Activities onClick Bitmap

So 1 have 2 ImageButtons, clothesButton1 with an image declared in xml, and imageButton2 which is blank. Both are in seperate activities.
Upon clicking clothesButton1, i want to move the image in clothesButton1 to imageButton2 using Bitmap. clothesButton1 will become blank afterwards.
Here's my code in Java for clothesButton1:
final ImageButton clothesButton1 =(ImageButton)findViewById(R.id.clothesBtn1);
clothesButton1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
clothesButton1.buildDrawingCache();
Bitmap bitmapclothes = clothesButton1.getDrawingCache();
Intent intent = new Intent();
intent.putExtra("BitmapClothes", bitmapclothes);
}
});
In my second activity (for imageButton2):
final ImageButton imageButton2 = (ImageButton)findViewById(R.id.imageButton2);
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapClothes");
imageButton2.setImageBitmap(bitmap);
However the moving function isn't working and I really have no idea where I am wrong. Any help is greatly appreciated.
The error is because of Intent. There are two types of Intent in android Explicit and implicit intents. When you create an Intent with a Context and a Class object, you are creating an explicit intent. You use explicit intents to start activities within your application.When an activity in your application wants to start an activity in another application, you create an implicit intent. In Your case it is Explicit Intent use Intent intent=new Intent(getApplicationContext(), secondActivityName.class).
In your case
Intent intent=new Intent(getApplicationContext(),secondActivityName.class);
intent.putExtra("BitmapClothes", bitmapclothes);
startActivity(intent);
To understand more about intent read this tutorial.
I think the problem is that you didn't enabled the drawing cache on clothesButton1.
clothesButton1.setDrawingCacheEnabled(true);
Please refer:https://developer.android.com/reference/android/view/View.html#getDrawingCache%28boolean%29
I think that you can achieve this easily with Activity Transition. Take a good look at it :)
The Start an activity with a shared element section might be what you want.
First,getDrawingCache() returns null;second the way you invoke other activity is not correct!
try this :
clothesButton1.setDrawingCacheEnabled(true);
clothesButton1.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
clothesButton1.layout(0, 0, clothesButton1.getMeasuredWidth(), imageButton.getMeasuredHeight());
clothesButton1.buildDrawingCache(true);
Bitmap bitmapclothes = Bitmap.createBitmap(clothesButton1.getDrawingCache());
clothesButton1.setDrawingCacheEnabled(false);
Intent intent = new Intent(NowActivity.this,SecondActivity.class);
intent.putExtra("BitmapClothes", bitmapclothes);
startActivity(intent);
Reference:Android View.getDrawingCache returns null, only null

Insert and read data from Sqlite over SQLiteOpenHelper

I have question about passing data between activities. Is possible from main activity open few next activities, and return result from last opened to root activity and how? I've added screenshot for better vision what I mean. Thanks for answers.
enter image description here
working with this question #Sahil Lombar
Intent i = new Intent(this, FirstActivity.class);
i.putExtra("data", "data");
startActivity(i);
in other activity we can
//firt validate if value extra "data" it was sent
if(getIntent().hashExtra("data") {
String value = getIntent().getStringExtra("data");
Log.d("SecondActivity", value);
}
working with String, int, boolean, double, Byte and array of these
more.. https://developer.android.com/reference/android/content/Intent.html
Start the First Activity with Intent holding the result;
In Activity 3
Intent i = new Intent(this, FirstActivity.class);
i.putExtra("data", "data");
startActivity(i);
finish();
In Activity 1 receive the result in public void onNewIntent or if you are starting a new activity in the onCreate method
public void onCreate(){
super.onCreate();
Intent i = getIntent();
String result = i.getStringExtra("data");
}

Copy intent / change source activity and destination class

I want to change to a new Activity over an Intent, but in my current Intent there are all my ExtraStrings as parameters. So decided to copy it with
Intent next = getIntent();
but how to change the source activity and the destiantion class now ?
Probably the better solution would be create your own Intent with your destination class and use Intent.putExtras(Intent src) to put extra data of original intent to new one.
And of course you can use Intent.setClass or setClassName() just to replace destination class.
You can also pass your data in bundle..
FirstActivity:
Intent mIntent = new Intent(this, activity1.class);
Bundle mBundle = new Bundle();
mBundle.putString("key", "value");
mIntent.putExtra("keyBundle", mBundle);
SecondActivity:
Intent mIntent = new Intent(this, activity2.class);
mIntent.putExtras(getIntent().getExtras().getBundle("keyBundle"));

Categories

Resources