android data passing and database access - android

I am experiencing a problem that when the Android device wakes up from sleep, the Activity would take forever to get redrawn (and I have to terminate it most of the time). I am not sure why, but when I comment out the code below, where it retrieves an object from the database based on id stored in the bundle, the problem goes away.
I am not sure why the db transaction is causing an issue. Any ideas?
Secondly, is it better to store the object in a bundle instead of storing its id and retrieving it from db in onCreate?
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.track_act);
/*
final Bundle extras=getIntent().getExtras();
long actId=extras.getLong("activity_id");
System.err.println("actId is "+actId);
Data.Activity act=DBManager.getActivity(actId, this);
*/
}

Are you getting nothing in LogCat related to this problem?
It looks like your getActivity method is a static method. Could there be some problems associated with that static reference after waking up from sleep?

Related

I can't see my logs in Android Studio logcat

Here's a simple app, I'm trying to create logs in the printToLogs method.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("Log0","test");
}
public void printToLogs(View view){
TextView menuTextView1 = findViewById(R.id.menu_item_1);
TextView menuTextView2 = findViewById(R.id.menu_item_2);
TextView menuTextView3 = findViewById(R.id.menu_item_3);
String item1 = (String) menuTextView1.getText();
String item2 = (String) menuTextView2.getText();
String item3 = (String) menuTextView3.getText();
Log.v("Log1",item1);
Log.v("Log2",item2);
Log.v("Log3",item3);
}
but logs with the tags Log1, Log2, Log3 are not shown at all in the logcat, what does show up is the Log0 in the onCreate method, but the other ones in printToLogs never show up when I search for them at all. I attempted re-installing the app and restarting logging. That didn't work.
The menu items are: Mango sorbet, Blueberry pie, Chocolate lava cake. And yes, I tried searching for them, and they are not in the logcat either.
If this is your actual code, you aren't even calling printToLogs in the onCreate method. You should be more diligent before posting something simple like this.
Barring a serious runtime environment issue, this problem should be fairly easy to solve.
It seems as if the printToLogs(View view) method is to be executed in response to the user clicking a button. If so, try including the following line in your activity_main.xml if you haven't already:
android:onClick="printToLogs"
This will bind the button on the UI with the printToLogs(View view) method.
If, on the other hand, printToLogs(View view) is intended to be a standalone method (i.e. one that should execute regardless of user input) it should not accept a View as an argument. For your purposes, its parameter list should be completely empty. In other words, the method signature should read:
public void printToLogs()
Also, it should be called within the onCreate(Bundle savedInstances) method. Add the following to the onCreate(Bundle savedInstances) method:
printToLogs();
This will initiate the execution of the method as soon as the app begins to run.
Make sure the logcat filter is set to "Verbose" when testing like so: (img is link since apparently I need 10 rep. to embed images into answers directly)
logcat filter
heyy add your method/function name in your button by using property section or just android:onClick in your xml file and then it will be solved

Issue with Android fragments: getView() returns null

Our app has several fragments. In one of them user fills several TextEdit fields. When he finishes he presses a button in the ActionBar to save the data. The Action just calls a private method named "saveData" that fetches all data from the fields and submit it to our server.
We have many stack traces from our users showing that getView() returns null in method saveData, but for just a small part of them. For most of them there is no problem at all. We cant reproduce the problem and we cant understand what might be causing it. The code is pretty simple:
View vw = this.getView();
EditText et;
et = (EditText)vw.findViewById(R.id.editEmail);
String email = et.getText().toString().trim();
et = (EditText)vw.findViewById(R.id.editPassword);
String password = et.getText().toString().trim();
The action is added in osResume, see below:
public void onResume() {
super.onResume();
MainActivity act = (MainActivity)this.getActivity();
act.bar.removeAllActions();
act.bar.addAction(new SaveAction());
}
Any ideas? How can we reproduce it?
Can you tell from your logs whether the problem is always for the same users / devices ?
I see from the code that you have submitted that the view is in the same fragment - is that actually the case ?
It's POSSIBLE that a fragment no longer in view can have their view destroyed in order to free up resources. e.g.
getView() returns null
If I suspected that this might be the case then I would attempt to recreate the problem on a phone / tablet / emulator with limited resources.
Good luck !

Android: setImgVisibility() error

In my app I have a header with icon hidden, I have a adapter with a listview when I click the listview I go to a login screen using listener, when the login is successful is should come back to listview(adapter) and icon should get visible on header.
In the login activity I have the following code:
public void onClick(View v) {
String password = etPassword.getText().toString();
if(password.equals("guest")){
SearchAdapter.setImgVisibility();
} else {
//-----
}
finish();
}
In my adapter I am calling the setImgVisibility() as follows, but it is not working
public static void setImgVisibility() {
img.setVisibility(View.VISIBLE);
}
I am getting a Nullpointerexception near the line img.setVisibility(View.VISIBLE);
I am stuck here and don't know what I am doing wrong. Any suggestions or help is appreciated
I would imagine that img is null. You need to look at where this value is set and make sure happens before you call the method setImgVisibility.
Show more of your complete code for people to help further.
Additionally, i've just noticed you've used a static reference to your search adapter, you should be really careful using statics, especially where any referencing of images is concerned as images can be bound to the context, as such unless you nullify the static you will end up with a memory leak. (this used to be an old problem, not sure its still valid, but i would still avoid using a static reference).
Without more code we're not likely to be able to properly help you. For example are you switching activities when logging in? If you are, this won't work at all.
[given the comment below] If you switch activities then your activity containing the list view is going to be destroyed and then rebuilt then you navigate back to it. or it will at least go through the activity lifecycle. This means you can set the icon during the instantiation of the header img.
You could store your logged in state as a property of the Application or a preference. Grab this value when you set the header image and set the image accordingly.
your img object is null. Is your img object is same as View v then you can pass v in setImgVisibility() and then set v.setVisibility(View.VISIBLE)

How to use startmanagingcursor in fragment?

How can I use startmanagingcursor in fragment? Because I need to pull data from SQLite in Fragment but I cannot use startmanagingcursor in it.
Here is my coding
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbUtil.open();
Cursor cursor = dbUtil.fetchNews(getArguments().getString("title"));
getActivity().startManagingCursor(cursor);
newsTitle = cursor.getString(0);
dbUtil.close();
}
Since everyone else seems to want to preach at you and not give you an answer, I'll actually answer your question (then preach :p).
getActivity().startManagingCursor(yourCursor);
It's as simple as that.
As others have noted, it's deprecated and a CursorLoader is recommended for many reasons... keeping data processing off the UI thread being the major one.
But if you truly need/want to use startManagingCursor in a fragment, the above snippet is how you would do it.
Given the further information you have posted, another issue might be where you are trying to call your database from. You should not use the onCreate method in a fragment class (at least for these operations), you should use onActivityCreated. That way you are certain that the activity that controls your fragment has finished setting itself up before you try and use anything associated with it.

Android memory usage

i am building an android application but i have some questions about
the memory usage.
Most of the data i need and use are string arrays stored in the xml strings file. I used arrays because firstly the biggest array will have up to 30 items and secondly there won't be any updating or deleting or inserting items through the app.
All the custom adapters i created are following googles' guidlines (the fast way - using the holder class)
As the user switches between the activities, depending on the selections he makes different arrays are loaded in the listviews.
Does android clears the memory each array allocates if its not in use? should i do that?
I ve used MAT also to check how the app uses memory and to check for leaks etc..and i thing that everything is fine. I also use a few png icons/images.
The app gets 5MB when it starts, goes up and down up to 8.5-9MB as the user plays around.
Thanks in advance for any help!
It's possible the Android OS will kill your Activities (without focus) on the stack if memory is needed. When your Activity is killed in this way, onSaveInstanceState(Bundle outState) is called. You should save your string array here.
When onCreate(Bundle savedInstanceState) is called in your Activity, if savedInstanceState is not NULL, then it means your Activity was previously killed by the OS and you need to repopulate your string array from that bundle.
ex:
String [] stringArray;
...
protected void onCreate(Bundle savedInstanceState)
{
if (savedInstanceState != null)
{
stringArray = savedInstanceState.getStringArray("some_key");
}
}
protected void onSaveInstanceState (Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putStringArray("some_key", stringArray);
}
This is described in more detail here: http://developer.android.com/reference/android/app/Activity.html

Categories

Resources