1

I have an Android application with 2 activities. Each activity has an AsyncTask that communicates with a server using UDP.

 public class MainPostTask extends AsyncTask<String, Integer, String>  {

        @Override
        protected String doInBackground(String... params) 
        { 
           ..... udp comunication here  
        }
  } 

I change between these two activities every 20 seconds.

e.g.:
Intent it = new Intent(this, MainActivity.class);
startActivity(it);

The other PostTask is omitted to reduce the text.

After 3 hours, the Android becomes very slow. Sometimes is very hard to close the application. I am sure that I am finishing the AsyncTask before changing activity.

Do you have any idea what is going on? How can I profile the Android to see the memory/cpu and etc?

I also use Log.d(). When I plug the cable all the messages buffered are output to the logcat. Do you think this log messages are causing the problem?

Best regards, Andersan.

Eenvincible
  • 5,641
  • 2
  • 27
  • 46
user3068649
  • 411
  • 3
  • 13

2 Answers2

1

Are you always changing activities by calling startActivity? If so, you're stacking activities. Don't do that.

What are you trying to accomplish by switching tasks? Answering that will let us know how to best handle your situation.

bryan
  • 798
  • 7
  • 18
  • yes, I always change activity calling startActivity(). Each 20 seconds I connect to a server and change the screen for 5 seconds. After that I return to the main screen. I repeat this process forever. How can i change activity without call start Activity()? I am new in Android. Best regards – user3068649 Oct 21 '15 at 20:50
  • If you want to change the screen, just use View.setVisibility(View.GONE) and View.setVisibility(View.VISIBLE). You'll have a single layout for your single activity. The layout will have two big subviews. Just toggle between those. Or when returning, call finish() instead of calling the MainActivity. – bryan Oct 21 '15 at 20:57
  • The problem I have 2 layouts (.xml). Is not there any other way to change the activities? – user3068649 Oct 21 '15 at 21:03
  • Now i am calling finish() in the function onPause(); Is it gonna work? regards – user3068649 Oct 21 '15 at 21:18
  • I can't see your code, but I'm going to guess "No." What would cause the onPause to be called? Really, you should merge the two layouts into one and have a single activity. Don't create a new activity just because you want to do something. AsyncTask can also run in the background. – bryan Oct 22 '15 at 20:24
0

You gave few code. Maybe you do not close the connection. Perhaps your task is not removed and fixated. Study the following materials:

Profiling with Traceview and dmtracedump (SO source)

Community
  • 1
  • 1
user2413972
  • 1,355
  • 2
  • 9
  • 25