I have the following situation:
Activity A starts Activity B, by using startActivityForResult
Activity B then returns an ArrayList of Strings to Activity A by using on finish().
Here is a code example of what exactly Activity B does:
ArrayList<String> urls = new ArrayList<>();
urls.add("Some string");
Intent intent = new Intent();
intent.putStringArrayListExtra(KEY, urls);
setResult(Activity.RESULT_OK, intent);
finish();
Then Activity A receive the data in onActivityResult(...)
The issue I have is that when the user taps the done button and Activity B's code example executes, Activity B freezes for about 3 seconds (when I have about 2 strings in the ArrayList). The more strings I have in the ArrayList the longer it freezes. I have more or less determined that it is finish() that causes the UI thread to freeze.
Is there a way to call finish() without freezing Activity B? If not, why is this happening?
EDIT:
Here is the full example:
/**
* Background task
*/
private class gatherUrlsTask extends AsyncTask<ArrayList<PictureEntry>, Integer, Intent> {
#Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
bt_done.setVisibility(View.GONE);
fab_add_picture.setVisibility(View.GONE);
}
#SafeVarargs
#Override
protected final Intent doInBackground(ArrayList<PictureEntry>... params) {
ArrayList<String> imagePaths = new ArrayList<>();
for (PictureEntry pictureEntry : params[0]) {
if (pictureEntry.isSelected()) {
imagePaths.add(pictureEntry.getPath());
}
}
if (imagePaths.size() == 0) {
Toast.makeText(getApplicationContext(), R.string.please_select_atleast_one_image, Toast.LENGTH_LONG).show();
return null;
} else {
Intent intent = new Intent();
intent.putStringArrayListExtra(SELECTED_IMAGES_KEY, imagePaths);
return intent;
}
}
#Override
protected void onPostExecute(Intent intent) {
setResult(Activity.RESULT_OK, intent);
finish();
}
}
However I can remove everything from the AsyncTask since it did not have any effect on performance.
i don't know what is cause of that, but for prevent ui getting freezed, use asyncTask and then in the onPostExcecute call finish()
Related
So im getting my data from a web service and using RecyclerView adapter.In RecyclerView.adapter's onBindviewholder method I want to pass the data to the recyclerView in the MainActivity but also pass the data (item) to MyMapsActivity.The thing is the onBindViewholder has setOnClickListener and
setOnLongClickListener as controls that are being used. Is there either a way to send the data(item) using an intent that doesnt start the activity or is there a way I can wire up a new button within the onBindViewholder method because what happens is when the app starts up it goes straight to MyMapsActivity which is expected. Is there a way to control this by either using a new button that can interact with onBindViewholder or is there a way to pass the intent.putExtra without starting the MyMapsActivity.Maybe My understanding is flawed but here is my code for the methods and activities stated:
onBindViewholder
public void onBindViewHolder(DataItemAdapter.ViewHolder holder, int position) {
final DataItem item = mItems.get(position);
try {
holder.titletext.setText(item.getTitle());
holder.companyText.setText(item.getCompany());
holder.cityText.setText(item.getCity());
holder.salarytext.setText(""+ item.getSalary());
holder.descriptionText.setText(item.getDescription());
holder.responsibilityText.setText(item.getResponsibility());
holder.latText.setText(""+ item.getLatitude());
holder.lngText.setText(""+ item.getLongitude());
holder.phoneText.setText(item.getPhone());
holder.provinceText.setText(item.getProvince());
} catch (Exception e) {
e.printStackTrace();
}
//click
holder.mView.setOnClickListener(new View.OnClickListener() {//getting viewholder class and ctor
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext,DetailsActivity.class);
intent.putExtra(ITEM_KEY,item);
mContext.startActivity(intent);
}
});
//long click
holder.mView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Toast.makeText(mContext, "long click: " + item.getTitle(), Toast.LENGTH_SHORT).show();
return false;
}
});
// !!!!!----this is the intent Im talking about----!!!
Intent intent = new Intent(mContext,MyMapActivity.class);
intent.putExtra(ITEM_KEY,item);
mContext.startActivity(intent);
}
My maps method :
public void onMapReady(GoogleMap googleMap) {
final DataItem item = getIntent().getExtras()
.getParcelable(DataItemAdapter.ITEM_KEY); //--------gets intent frm dataItemAdapter
if (item == null) {
throw new AssertionError("null data recived");
}
mapReady = true;
mMap = googleMap;
LatLng latLngToronto = new LatLng(43.733092, -79.264254);
// LatLng latLnghome = new LatLng(43.656729, -79.377162);
CameraPosition target = CameraPosition.builder().target(latLngToronto).zoom(15).tilt(65).build();
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(target));
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
markerDisplay(item.getTitle(),item.getLatitude(),item.getLongitude());//------maps jobs marker-------new
//add markers and
//instantiate
// mMap.addMarker(mp);
// mMap.addMarker(md);
}
You do not need to call startAcivity method.
There is sendBroadcast method for sharing intent to other components.
String CUSTOM_ACTION = "com.example.YOUR_ACTION";
Intent i = new Intent();
i.setAction(CUSTOM_ACTION)
intent.putExtra(ITEM_KEY,item);
context.sendBroadcast(intent);
Then you can receive your intent with BroadcastReceiver
public class MyReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action!=null && action.equals(CUSTOM_ACTION)){
//do something
}
}
}
register receiver with custom IntentFilter into onCreate method of your activity
IntentFilter filter = new IntentFilter(CUSTOM_ACTION);
registerReceiver(myReceiver, filter);
And do not forget unregister it in onDestroy
unregisterReceiver(myReceiver);
P.S
There are many ways of transferring data between classes and components. It depends on your purpose.
Broadcast receiver with intents are good for data transfer between services and activities and for data transfer between apps.
If you need to have access to data from different places then there are other ways.
If the data needs to be stored on a disk, then the database is suitable.
To store data in RAM, you can use separate class and store this class as a Singleton or into your Application class.
I've 2 activities, and need the main Activity to send some data to the second Activity then the second activity analyze this data and send response back, so I've the below 2 codes:
Main Activity:
import static tk.zillion.mobile.SecondActivity.EXTRA_STUFF;
public class MainActivity extends Activity {
private static int PICK_CONTACT_REQUEST = 0;
private static final int SECOND_ACTIVITY_RESULT_CODE = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Start the SecondActivity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra(Intent.EXTRA_TEXT, "my text");
startActivityForResult(intent, SECOND_ACTIVITY_RESULT_CODE);
finish();
}
// This method is called when the second activity finishes
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(this, "I'm the Main activity", Toast.LENGTH_SHORT).show();
// check that it is the SecondActivity with an OK result
if (requestCode == SECOND_ACTIVITY_RESULT_CODE) {
if (resultCode == RESULT_OK) {
// get String data from Intent
String returnString = data.getStringExtra(EXTRA_STUFF);
// set text view with string
Toast.makeText(this, "I'm the Main activity", Toast.LENGTH_SHORT).show();
}
}
}
and the Second Activity is as below:
public class SecondActivity extends Activity {
static final String EXTRA_STUFF = "tk.zillion.mobile.EXTRA_STUFF";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent();
String s = getIntent().getStringExtra(Intent.EXTRA_TEXT);
Toast.makeText(this, "I'm the second activity " + s , Toast.LENGTH_SHORT).show();
Bundle basket =new Bundle();
basket.putString(EXTRA_STUFF, s);
intent.putExtras(basket);
setResult(RESULT_OK, intent);
finish();
}
}
The second activity is fired correctly, and receive the data from the main one, but once the data is sent back the onActivityResult is not fired!!
How can I solve it? thanks
Remove the finish() call that comes after startActivityForResult(). You prevent the oncoming activity to give a result back, because you remove your MainActivity with finish() from the back task.
Dont call finish() after startActivityForResult otherwise that instance of MainActivity that started the second one will be destroyed
I have a Fragment MainFragment and I do:
Intent i = new Intent(getActivity(), PersonActivity.class);
startActivityForResult(i, 0);
The activity starts ok and it starts its own PersonFragment and inside the PersonFragment I do:
#Override
public void onDestroy() {
super.onDestroy();
Intent i = new Intent();
i.putExtra(PERSON_ID_EXTRA, getPersonId());
i.putParcelableArrayListExtra(PERSON_CONTACT_LIST, (ArrayList<? extends Parcelable>) contactFriends);
getActivity().setResult(Activity.RESULT_OK, i);
}
Back in my MainFragment I do:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if ( requestCode != 0) {
return;
}
int personId = data.getIntExtra(PERSON_ID_EXTRA, -1);
List<Person> contacts = data.getParcelableArrayListExtra(PERSON_CONTACT_LIST);
for(Person p:contacts) {
Log.d("APP", p.getFullName());
}
}
I see that the code goes to onActivityResult but the data is null. What am I messing up here?
Update:
I see that pressing back button does not call onDestroy().
But where am all examples I saw used getActivity.finish() and I don't want to finish the activity. Only when the user presses e.g. back send the data
Update2:
I added the following and I go through that code but the Intent data in the result onActivityResult is still null
#Override
public void onPause() {
super.onPause();
Intent i = new Intent();
i.putExtra(PERSON_ID_EXTRA, getPersonId());
i.putParcelableArrayListExtra(PERSON_CONTACT_LIST, (ArrayList<? extends Parcelable>) contactFriends);
getActivity().setResult(Activity.RESULT_OK, i);
}
From the Activity documentation about finish:
Call this when your activity is done and should be closed. The
ActivityResult is propagated back to whoever launched you via
onActivityResult().
From the documentation about onActivityResult:
Called when an activity you launched exits, giving you the requestCode
you started it with, the resultCode it returned, and any additional
data from it.
So, onActivityResult will only be called when the second activity finishes.
If you don't want to finish your PersonActivity to send the result to your main activity, then you may want to start another intent to send the data or pass the data using static fields (not a best practice at all).
In your case, to set your result when you press the back button, you can write a code like this:
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
Intent i = new Intent();
i.putExtra(PERSON_ID_EXTRA, getPersonId());
i.putParcelableArrayListExtra(PERSON_CONTACT_LIST, (ArrayList<? extends Parcelable>) contactFriends);
getActivity().setResult(Activity.RESULT_OK, i);
finish();
return true;
}
return false;
}
Follow up based on the comments:
The code for the finish() on the Activity class looks as follows:
// Call this when your activity is done
// and should be closed. The ActivityResult
// is propagated back to whoever launched
// you via onActivityResult().
public void finish() {
if (mParent == null) {
int resultCode;
Intent resultData;
synchronized (this) {
resultCode = mResultCode;
resultData = mResultData;
}
if (false) Log.v(TAG, "Finishing self: token=" + mToken);
try {
if (resultData != null) {
resultData.prepareToLeaveProcess();
}
if (ActivityManagerNative.getDefault()
.finishActivity(mToken, resultCode, resultData)) {
mFinished = true;
}
} catch (RemoteException e) {
// Empty
}
} else {
mParent.finishFromChild(this);
}
}
Here you can see that is the responsibility for setting the result values lays on the finish() method.
Then, before the Activity is destroyed, the onDestroy() method is called. That is the reason why setting the result of the Activity on the onDestroy() method won't work.
I'm android beginner so please be easy on me. I'm doing some "exercises" and i'm writing simple app which will tell RSSI strength of home wifi network. Getting that number is pretty easy, but updating it and showing that on screen it's a little more complicated as i thought.
First this is my onCreate Activity. In this activity i'm launching another android component - Service. Because the code will run in background (i know i could use thread or something else, but this is for "practice" sake, and i have a few ideas what to do with this app, while running service and not interacting with UI )
public class MainActivity extends Activity {
TextView wifi_check;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
referenceViews();
startService(new Intent(this, CheckingWifiService.class));
//wifi_check.setText(""+getIntent().getExtras().getInt("RSSI"));
}
private void referenceViews() {
wifi_check = (TextView) findViewById(R.id.wifiCheck_TV);
}
}
Because my code will run every second or so, i will use TimerTask for this purpose. And here is my TimerTask class, which includes run() method, and code for executing inside
public class TimerTsk extends TimerTask {
Context act;
WifiManager wifiMan;
WifiInfo info;
Bundle sendInfo;
Intent intent;
int rssi;
public TimerTsk(Context context) {
act = context;
}
#Override
public void run() {
intent = new Intent();
sendInfo = new Bundle();
wifiMan = (WifiManager) act.getSystemService(Activity.WIFI_SERVICE);
info = wifiMan.getConnectionInfo();
rssi = info.getRssi();
Log.d("WORKED", "RUNNING SUCESSFULLY");
// i want to send info to my activity
sendInfo.putInt("RSSI", rssi);
intent.putExtras(sendInfo);
}
}
From this class , i want to send result of RSSI to my activity and then update a text. But when i call this code below, on activity i always get NullPointerException.
wifi_check.setText(""+getIntent().getExtras().getInt("RSSI"));
To be honest i had hard time figuring out which part of code is throwing an exepction. And i found that more exactly, this part of code is throwing an exepction.
getInt("RSSI")
Overall i see that service is running, because in my LOGCAT i see a message that i create with Log.d in TimerTsk class.
Any ideas why is this happening?
Here is my service class:
public class CheckingWifiService extends Service{
int rssi;
Timer time;
TimerTsk ttsk;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
time = new Timer();
time.schedule(new TimerTsk(getApplicationContext()), 500);
return START_STICKY;
}
}
Here is my LogCat:
I see a common mistake. Don't do this:
sendInfo.putInt("RSSI", rssi);
intent.putExtras(sendInfo); // This adds a Bundle to your existing Bundle!
You are creating an Intent, with a Bundle of extras, with a Bundle that holds rssi. Leave out this unnecessary Bundle:
intent.putExtras("RSSI", rssi);
Now in your next Activity you can use:
getIntent().getIntExtra("RSSI", 0);
However you should always check to make sure there aren't any surprise null variables:
Intent in = getIntent();
if(in != null) {
int rssi = in.getIntExtra("RSSI", -1);
if(rssi < 0)
wifi_check.setText(""+rssi);
else
wifi_check.setText("Unknown");
}
is your activity starting? I don't see any call to startActivity(). In any case as mentioned by Sam you just need to call putExtra for your intent. don't forget to call
is your activity starting? I don't see any call to startActivity(). In any case as mentioned by Sam you just need to call putExtra for your intent. don't forget to call
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
you need to put this flag when start activies from background
I'm writing an app that has multiple activities. Activity A calls Activity B, not expecting a result. Then if a button is pressed B startsActivityForResult with Activity C. When Activity C is done, it makes an intent with all of the extras it needs and finishes. The problem is that when it calls this.finish() or just finish(), it brings me all the way back out to Activity A. onActivityResult in Activity B is not called. What is wrong?
Activity A: Starts Activity B
Intent in = new Intent(ccstart.this,mainmenu.class);
in.putExtra("uid",loginresponse);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("usr",text_user.getText().toString());
// Commit the edits!
editor.commit();
startActivity(in);
Activity B: Starts Activity C for result
Intent intent = new Intent(mainmenu.this,filebrowser.class);
startActivityForResult(intent,0);
Activity C: Return statement
Intent result = new Intent();
result.putExtra("fname", file.getAbsolutePath());
this.setResult(Activity.RESULT_OK, result);
finish();
Activity B: Upon the result of activity c...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// If the request went well (OK) and the request was PICK_CONTACT_REQUEST
if (resultCode == Activity.RESULT_OK && requestCode==0) { //upload a file
final String fname = data.getExtras().getString("fname");
final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); //Load settings
final String uid = settings.getString("uid", "");
new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
doFileUpload(fname, uid);
}
}).start();
}
}
What is the issue with that? It happens with an activity that doesn't return a result as well, so its not just this one.
Thanks!
You need to explicit close your activity when you start the next one, if not, it stays in the "stack of activities" that you can access with the back button or when the next activity closes.
You need to call finish on activity A after you started activity B