So my application re-uses quite a few activities. Originally I was simply adding a "fromClass" line as an extra to Intents as I switched between activities. The problem now is that I need to know which class I started with several activities ago, in order to display information properly on the current activity. I was trying to use an arraylist of strings to store the order and remove them from the list as I went backwards, but I cannot get strings to translate into class names and work correctly.
At the start of the activity I'm adding the following way:
Global.appNavigation.add("SecondActivity.class")
When I call onBackPressed() I'm doing this:
/* Remove Last Object (this class) */
Globals.appNavigation
.remove(Globals.appNavigation.size() - 1);
try {
Class<?> c = Class.forName(Globals.applicationNavigation
.get(Globals.appNavigation.size() - 1));
Activity obj = (Activity) c.newInstance();
Intent i = new Intent(mActivity, c);
startActivity(i);
finish();
} catch (ClassNotFoundException e) {
Log.e("Back", "Could not get a class name");
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I'm constantly getting the ClassNotFoundException. Is there a better way to do this? This application I'm working with is getting quite large, so not calling finish() to an activity may not work in this case.
I ended up calling the "finish()" method, but did a better job keeping track of data that needed to be persistent.
Thanks to Ben Von Handorf for the suggestion (see comments 1 and 3 on original question)
Related
I am trying to save data into a Firebase RealtimeDatabase.
The process of saving is fine and works just well, but I was curious about an idea I had: To force the .setValue operation into a synchronous structure. I know, that that way isn't the best one, but I wanted to try it regardless of that fact.
So I came up with this code:
Thread t1 = new Thread(){
public void run(){
try {
Tasks.await(databaseReference.child("someChild").setValue(someObject));
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("finished");
}
In theory, that code snippet is intended to first set the value in the Database, then wait for the completion of that (through the Tasks.await()), and then print out **after that* the line "finished".
I wrapped the Tasks.await()-command in a second thread because when I don't, I get an exception which says something like "Tasks.await() must not be called in the main application thread".
But when I run the whole thing, it just gets stuck at t1.join();.
When I delete the t1.join();-statement, everything works just fine, but the second Thread (t1) isn't finished before the "finished"-output is there. I know why this is like that, but I am nontheless interested in a solution to that problem.
I'm developing an Android app that has to update it's UI depending on receiving and processing some server responses, I'm using runOnUiThread for that. I have like five activities in that app, all is working very well but one requires me to relaunch the Activity(like going to another one and then returning to it) or interacting with it in order to that update takes place, and that is the way i'm using with all the Activities including the infected one:
runOnUiThread(new Runnable() {
public void run() {
try {
response_received(response);
} catch (Exception e) {
e.printStackTrace(); // never catch any Exceptions
}
}
});
private static void response_received(JSONObject response) throws Exception{
try {
int volume_setted = response.getInt(volume);
Normal_Activity.volume_value.setText(String.valueOf(volume_setted)); // the Volume TextView updated efficiently
Infected_Activity.volume_value.setText(String.valueOf(volume_setted)); // has the problem mentioned above
} catch (JSONException ex) {
}
}
I'm pretty sure the problem is not in the TextView as all the Activity UI has this problem but i just posted an example.
Do not directly set values in a Activity from another Activity. If you want to pass data to Another activity always use Intents. check the below link
pass data from intent to an other intent
If you want to start another activity and get result back check the below link
http://developer.android.com/training/basics/intents/result.html
I'm really new with esspresso issues and I have a little question, I need to emulate that users click on a button, tree times, but with some delay. A Human, takes some time to click a button, maybe with one second delay.
Whats the better way to made that on an esspresso test? othere frameworks have sleep, and so on... but I think that espresso hasen't. Any idea?
--Edited:
I made this, but don't know if it's correct:
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Another way, but more clear, if you don't use thread.interrupt() is this:
SystemClock.sleep(millis)
for (int = i; i < numOfClicks; i++) {
onView(<matcher for your view>).perform(click());
SystemClock.sleep(1000);
}
This question already has answers here:
How can I avoid concurrency problems when using SQLite on Android?
(10 answers)
Closed 9 years ago.
I'm confused a little bit when tried to establish a multithreading work with my SQLite db, so I have a service which periodically load a data from server and insert it in different db tables, also any user in any time can store his own data in db when pressed "save button" in app windows, so based on the rules that only one thread at time can write a data in db I'd'd like to make a thread-cooperation. At first I have created a singleton which have only one db instance, and all goes pretty good with my read-db methods, cause all threads can read data in the same time, but what about writing? I use a thread inside write-function, and don't give a start another until previous thread has finished it work.(also I do it for calls from ui thread when user press save button)
Question: All I want to do is consider two situations - first is when threads call's same function to write data then I used synchronized, second - when threads call different write functions I should use a lock, right? So now I came to decision, but is it correct and right to do like that?
Code (Updated):
// Sync method for processing 1st situation
public synchronized void addPoints(final ArrayList<Point> points, final OnDBOperationListener listener) {
if (listener != null) {
// Lock for others write-threads in 2nd situaton
synchronized (mWriteLock) {
while (mWriteWait) {
try {
mWriteLock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
mWriteWait = true;
try {
SQLiteDatabase db = getDatabase();
// write a data
listener.onSuccess();
} catch (Exception e) {
e.printStackTrace();
listener.onError();
} finally {
closeDatabase();
synchronized (mWriteLock) {
mWriteWait = false;
mWriteLock.notifyAll();
}
}
}
}
}
After a long search i finally found a gret answer for my broblem, so anyone who want to creat e multithreading acess to db should read this first What are the best practices for SQLite on Android?
I get a small problem: I need using async task in cocos2d-x on Android.
private void parseJSONJava() throws ClientProtocolException, IOException, JSONException
{
STAJSONParser jPars = new STAJSONParser();
jPars.makeHttpRequest(String.format("%s/app/%s/json",STA_URL,STA_APP_UID));
}
But this code crash application with error Can't create handler inside thread that has not called Looper.prepare(). I solve this by adding runOnUiThread:
me.runOnUiThread(new Runnable(){
public void run(){
STAJSONParser jPars = new STAJSONParser();
jPars.makeHttpRequest(String.format("%s/app/%s/json",STA_URL,STA_APP_UID));
}
});
Where "me" is my Activity.
Code from STAJSONParser:
public JSONObject makeHttpRequest(String url) {
AsyncGetJson Task= new AsyncGetJson(url);
try {
return Task.execute().get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
AsyncGetJson task its a simple AsyncTask that get JSON from server.
So, my question: is this solution is right/wrong? Or you can give me other solution?
I don't see why you couldn't do that. You could also use libcurl like m.ding mentioned, along with pthreads and a json parser. But the problem there is that you'd need to manage the pthreads yourself. It's "messier" than just doing it the way you're doing it now. Then again, using the JNI isn't exactly pretty either. It's one big giant trade-off, probably leaning in favor of the JNI & Android Java SDK.
On iOS and Android, pthreads are the underlying threading mechanism, which are already managed for you when you use things like iOS's NSOperation and Android's AsyncTask (I'm assuming..)