gc freed xxxx objects and than freeze application - garbage-collection

I'm trying to create simple app which contains of ten activities. Each activity look pretty much the same, it has four buttons (different color) and when one particular button is clicked it's open next activity. OnCreate method of every activity has mediaplayer that play name of that activity. After some time I see in LogCat that gc freed some objects and at that time Activity don't play any sound and buttons are disabled.
Do you have any advice or suggestions how to resole that?
Here is code of one Activity:
public class Green extends Activity {
int buttonActive = 0;
int buttonWrong = 0;
MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_green);
buttonWrong = 0;
playSound();
Button btn = (Button)findViewById(R.id.button1);
Button btn2 = (Button)findViewById(R.id.button2);
Button btn3 = (Button)findViewById(R.id.button3);
Button btn4 = (Button)findViewById(R.id.button4);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(buttonActive == 1){
playSoundCorrect();
Intent intent = new Intent(v.getContext(),Blue.class);
startActivity(intent);
}
}
});
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(buttonActive == 1 && buttonWrong == 0){
buttonWrong = 1;
playSoundWrong();
}
}
});
btn3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(buttonActive == 1 && buttonWrong == 0){
buttonWrong = 1;
playSoundWrong();
}
}
});
btn4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(buttonActive == 1 && buttonWrong == 0){
buttonWrong = 1;
playSoundWrong();
}
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
private void playSound(){
MediaPlayer player = MediaPlayer.create(this, R.raw.green);
player.start();
player.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
buttonActive = 1;
}
});
}
private void playSoundWrong(){
player = MediaPlayer.create(this, R.raw.wrong2);
if(!player.isPlaying()){
player.start();
}
player.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
buttonWrong = 0;
}
});
}
private void playSoundCorrect(){
MediaPlayer player = MediaPlayer.create(this, R.raw.correct);
player.start();
player.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
}

It looks like in private void playSound you're masking the class's player property. If you say player = MediaPlayer.create then the class's player property will be set to the new MediaPlayer, and the player will be reachable so long as the class is reachable. Instead you're saying MediaPlayer player = MediaPlayer.create, which is creating a new player variable that is scoped to the playSound method - once the method terminates the player is no longer reachable and will be garbage-collected, even if the class is still reachable.

Related

TimePicker for each button inside RecyclerView

What I want to accomplish- The recyclerview has a list of apps and each row contains a button. When you click on the button, a timepicker shows up where you choose how long the countdown is for. Then the chosen time appears on the button. When you click the start button, countdowns begin and the time on the button counts down.
I have originally created a button in AppLimitScreen.java and accomplished the above without the button being inside recycler view:
public class AppLimitScreen extends AppCompatActivity {
private int numberOfInstalledApps;
private List<ApplicationInfo> apps;
private ArrayList<AppInfo> res;
//for countdown
static Button chooseTime;
static int Chour, Cminute;
static TimePickerDialog timePickerDialog;
DialogFragment dialogFragment;
private CountDownTimer mCountDownTimer;
static ImageView image;
private Button mButtonStart;
private List<AppLimit> appLimitList = new ArrayList<>();
private RecyclerView recyclerView;
private AppLimitAdapter alAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_limit_screen);
numberOfInstalledApps = getPackageManager().getInstalledApplications(0).size();
apps = getPackageManager().getInstalledApplications(0);
res = new ArrayList<AppInfo>();
mButtonStart = findViewById(R.id.startTimes);
chooseTime = (Button) findViewById(R.id.app_button1);
image = (ImageView) findViewById(R.id.imageView1);
image.setVisibility(View.VISIBLE);
Chour = 0;
Cminute = 0;
chooseTime.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialogFragment = new TimePickerclass();
dialogFragment.show(getSupportFragmentManager(), "Time Picker");
}
});
mButtonStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startTimer();
}
});
updateCountDownText();
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
alAdapter = new AppLimitAdapter(appLimitList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
recyclerView.setAdapter(alAdapter);
prepareAppLimitData();
}
private static long START_TIME_IN_MILLIS = 0;
private static long mTimeLeftInMillis = START_TIME_IN_MILLIS;
public static class TimePickerclass extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState){
timePickerDialog = new TimePickerDialog(getActivity(),
AlertDialog.THEME_HOLO_LIGHT,this,Chour,Cminute,true);
// timePickerDialog4.setButton(DialogInterface.BUTTON_POSITIVE, "Start", timePickerDialog4);
return timePickerDialog;
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute){
chooseTime.setText(hourOfDay + "h " + minute + "m" + " 00s");
START_TIME_IN_MILLIS = (hourOfDay * 3600000) + (minute * 60000);
mTimeLeftInMillis = START_TIME_IN_MILLIS;
image.setVisibility(View.INVISIBLE);
}
}
private void updateCountDownText() {
int hours = (int) ((mTimeLeftInMillis / 1000) / 60) / 60;
int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
if (minutes > 59) {
minutes = (minutes % 60);
hours += (minutes / 60);
}
String timeLeftFormatted = String.format(Locale.getDefault(), "%02dh %02dm %02ds", hours, minutes, seconds);
chooseTime.setText(timeLeftFormatted);
if (hours != 0 || minutes != 0 || seconds != 0) {
image.setVisibility(View.INVISIBLE);
chooseTime.setClickable(false);
}
else {
image.setVisibility(View.VISIBLE);
chooseTime.setClickable(true);
chooseTime.setText("");
}
}
private void startTimer() {
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
#Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
}
#Override
public void onFinish() {}
}.start();
}
private void prepareAppLimitData() {
AppLimit appLimit;
for (int i = 0; i < apps.size(); i++) {
if (getPackageManager().getLaunchIntentForPackage(apps.get(i).packageName) != null) {
// Non-System App
ApplicationInfo p = apps.get(i);
AppInfo newInfo = new AppInfo();
newInfo.appname = p.loadLabel(getPackageManager()).toString();
newInfo.icon = p.loadIcon(getPackageManager());
res.add(newInfo);
appLimit = new AppLimit(newInfo.appname, newInfo.icon);
appLimitList.add(appLimit);
} else {
// System Apps
}
}
alAdapter.notifyDataSetChanged();
}
}
Then I created the recyclerview and included a button for each row. The code for the AppLimitAdapter is shown below:
public class AppLimitAdapter extends RecyclerView.Adapter<AppLimitAdapter.MyViewHolder> {
private List<AppLimit> appLimitList;
private Context context;
public AppLimitAdapter(Context context) {
this.context = context;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public ImageView icon;
public Button button;
public ImageView image;
public MyViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.name);
icon = (ImageView) view.findViewById(R.id.icon);
image = (ImageView) view.findViewById(R.id.imageTimer);
button = (Button) view.findViewById(R.id.button);
}
}
public AppLimitAdapter(List<AppLimit> appLimitList) {
this.appLimitList = appLimitList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.app_limit_list_row, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
AppLimit appLimit = appLimitList.get(position);
holder.name.setText(appLimit.getName());
holder.icon.setImageDrawable(appLimit.getIcon());
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// when each button is clicked, action goes here
}
});
}
#Override
public int getItemCount() {
return appLimitList.size();
}
#Override
public int getItemViewType(int position) {
return position;
}
}
I have tried copying the code from AppLimitScreen.java and placing it inside onClick within onBindViewHolder, but I get errors and I'm sure there's a better way to do this.
How should I implement this?
What I want to accomplish- The recyclerview has a list of apps and each row contains a button. When you click on the button, a timepicker shows up where you choose how long the countdown is for. Then the chosen time appears on the button. When you click the start button, countdowns begin and the time on the button counts down.

Counting number of times android app goes into background

I'm developing an app which counts number of times the app goes to the background. It also retains the values when the orientation is changed. Though I have it working of all use cases, I have one use case which does not work.
Case : When I press the home button, change the phone's orientation and reopen the app, It does open in landscape mode but, the background count does not increase.
I have tried setting values in all the life cycle methods. It doesn't work. Hope somebody can help me with this.
`
public class MainActivity extends AppCompatActivity {
private int clickCount =0, backgroundCount = 0;
TextView tvClickCountValue, tvBackgroundCountValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if( savedInstanceState != null){
clickCount = savedInstanceState.getInt("COUNT");
backgroundCount = savedInstanceState.getInt("BGCOUNT");
}
setContentView(R.layout.activity_main);
tvClickCountValue = (TextView) this.findViewById(R.id.tvClickCountValue);
tvBackgroundCountValue = (TextView) this.findViewById(R.id.tvBackgroundCountValue);
setView(MainActivity.this);
}
public void onClick(View v){
clickCount += 1;
tvClickCountValue.setText(Integer.toString(clickCount));
}
public void setView(Context ctx){
tvClickCountValue.setText(Integer.toString(clickCount));
tvBackgroundCountValue.setText(Integer.toString(backgroundCount));
}
#Override
protected void onStop() {
super.onStop();
backgroundCount += 1;
}
#Override
protected void onResume() {
super.onResume();
tvClickCountValue.setText(Integer.toString(clickCount));
tvBackgroundCountValue.setText(Integer.toString(backgroundCount));
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("COUNT", clickCount);
outState.putInt("BGCOUNT", backgroundCount);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
clickCount = savedInstanceState.getInt("COUNT");
backgroundCount = savedInstanceState.getInt("BGCOUNT");
}
}
This article contains useful information: https://developer.android.com/guide/topics/resources/runtime-changes.html especially in the section about Handling The Change.
Try handling it on onConfigurationChanged() of you have disabled Activity restarts on orientation changes. Otherwise, probably through this article you will find which case applies to your scenario.
By reading the problem description, I am assuming that if you don't rotate the device the application works as intended.
you need to persist the count in the SharedPreferences. each time you reopen the app read the last value from the SharedPreferences. And increment and save to SharedPreferences each time the app is hidden.
Then you can count the with #kiran code:
public class BaseActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
public static boolean isAppInFg = false;
public static boolean isScrInFg = false;
public static boolean isChangeScrFg = false;
#Override
protected void onStart() {
if (!isAppInFg) {
isAppInFg = true;
isChangeScrFg = false;
onAppStart();
}
else {
isChangeScrFg = true;
}
isScrInFg = true;
super.onStart();
}
#Override
protected void onStop() {
super.onStop();
if (!isScrInFg || !isChangeScrFg) {
isAppInFg = false;
onAppPause();
}
isScrInFg = false;
}
public void onAppStart() {
// app in the foreground
// show the count here.
}
public void onAppPause() {
// app in the background
// start counting here.
}
}

How to destroy activity without crashing the app

I`v been trying for a while to destroy activity when the home button is pressed or when the app is in background and I manage to do something with this code
#Override
protected void onPause() {
this.finish();
super.onPause();
}
and when the app runs it does not crash but after a while playing in the activity the app just goes background and crashes.
This is the activity that I`m talking about:
public class Playing extends AppCompatActivity implements View.OnClickListener {
final static long INTERVAL=1154;//1 second
final static long TIMEOUT=7000;
int progressValue = 0;
CountDownTimer mcountDown;
List<Question> questionPlay = new ArrayList<>();
DbHelper db;
int index=0 , score =0,thisQuestion=0,totalQuestion,CorrectAnswer;
String mode = "";
ProgressBar progressBar;
ImageView imageView;
Button btnA, btnB, btnC,btnD;
TextView txtScore,txtQuestion;
#Override
protected void onPause() {
this.finish();
super.onPause();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playing);
Bundle extra = getIntent().getExtras();
if(extra!=null)
mode =extra.getString("MODE");
db = new DbHelper(this);
txtScore = (TextView)findViewById(R.id.txtScore);
txtQuestion =(TextView)findViewById(R.id.txtQuestion);
progressBar=(ProgressBar) findViewById(R.id.progreessBar);
btnA = (Button) findViewById(R.id.btnAnswerA);
btnB =(Button)findViewById(R.id.btnAnswerB);
btnC = (Button)findViewById(R.id.btnAnswerC);
btnD = (Button)findViewById(R.id.btnAnswerD);
imageView = (ImageView)findViewById(R.id.question_bike);
btnA.setOnClickListener(this);
btnB.setOnClickListener(this);
btnC.setOnClickListener(this);
btnD.setOnClickListener(this);
}
#Override
protected void onResume() {
super.onResume();
questionPlay = db.getQuestionMode(mode);
totalQuestion = questionPlay.size();
mcountDown = new CountDownTimer(TIMEOUT, INTERVAL) {
#Override
public void onTick(long millisUntilFinished) {
progressBar.setProgress(progressValue);
progressValue++;
}
#Override
public void onFinish() {
mcountDown.cancel();
showQuestion(++index);
}
};
showQuestion(index);
}
private void showQuestion(int index) {
if (index < totalQuestion){
thisQuestion++;
txtQuestion.setText(String.format("%d %d",thisQuestion,totalQuestion));
progressBar.setProgress(0);
progressValue = 0;
int ImageID = this.getResources().getIdentifier(questionPlay.get(index).getImage().toLowerCase(),"drawable",getPackageName());
imageView.setBackgroundResource(ImageID);
btnA.setText(questionPlay.get(index).getAnswerA());
btnB.setText(questionPlay.get(index).getAnswerB());
btnC.setText(questionPlay.get(index).getAnswerC());
btnD.setText(questionPlay.get(index).getAnswerD());
mcountDown.start();
}
else {
Intent intent = new Intent(this,Done.class);
Bundle dataSend = new Bundle();
dataSend.putInt("SCORE", score);
dataSend.putInt("TOTAL",totalQuestion);
dataSend.putInt("CORRECT",CorrectAnswer);
intent.putExtras(dataSend);
startActivity(intent);
finish();
}
}
#Override
public void onClick(View v) {
mcountDown.cancel();
if (index < totalQuestion){
Button clickedButton = (Button)v;
if(clickedButton.getText().equals(questionPlay.get(index).getCorrectAnswer())){
score+=1;
CorrectAnswer++;
showQuestion(++index);
}
else showQuestion(++index);
txtScore.setText(String.format("S:%d",score));
}
}
#Override
public void onBackPressed() {
Intent intent = new Intent(Playing.this ,MainActivity.class) ;
startActivity(intent) ;
finish() ;
}
}
I`v tried and with this code
#Override
protected void onDestroy(){
super.onDestroy;
finish();
}
but then the second activity does not start and the app crashes again.
So what to do?

Android studio pause and resume countdown timer for progress bar

I am using a countdown timer which updates my progress bar which decreases from 100 (full) to 0(empty) but when the app is paused would like to pause the timer and then restart it upon resuming the app. I implement the count down timer like so:
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisUntilFinished) {
int progress = (int) (millisUntilFinished / 100);
progressBar.setProgress(progress);
}
#Override
public void onFinish() {
progressBar.setProgress(0);
Intent intent = new Intent(getApplicationContext(), GameOver.class);
startActivity(intent);
finish();
}
}
And in my on create:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_screen);
progressBar = (ProgressBar)findViewById(R.id.timerBar);
progressBar.setProgress(100);
myCountDownTimer = new MyCountDownTimer(10000, 10);
myCountDownTimer.start();
}
You can add a boolean in your MyCountDownTimer class that will change in the onPause and onResume methods of your Activity. Check the // ADDED comments to see the changes you have to make to make this work.
public class MyActivity extends Activity {
MyCountDownTimer myCountDownTimer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_screen);
progressBar = (ProgressBar)findViewById(R.id.timerBar);
progressBar.setProgress(100);
myCountDownTimer = new MyCountDownTimer(10000, 10);
myCountDownTimer.start();
}
// ADDED
#Override
public void onPause() {
super.onPause(); // Always call the superclass method first
myCountDownTimer.pause();
}
// ADDED
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
myCountDownTimer.resume();
}
}
public class MyCountDownTimer extends CountDownTimer {
// ADDED
private boolean pause;
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
// ADDED
public void pause() {
pause = true;
}
// ADDED
public void resume() {
pause = false;
}
#Override
public void onTick(long millisUntilFinished) {
int progress = (int) (millisUntilFinished / 100);
// ADDED
if (!pause) {
progressBar.setProgress(progress);
}
}
#Override
public void onFinish() {
// ADDED
isRunning = false;
progressBar.setProgress(0);
Intent intent = new Intent(getApplicationContext(), GameOver.class);
startActivity(intent);
finish();
}
}

dialog.show() crashes my application, why?

I'm new in adroid.
I like to do things when the color reach a value. I like (for example) show the alert if r is bigger than 30, but the application go in crash. Thank for very simple answares.
public class MainActivity extends Activity {
private AlertDialog dialog;
private AlertDialog.Builder builder;
private BackgroundColors view;
public class BackgroundColors extends SurfaceView implements Runnable {
public int grand=0;
public int step=0;
private boolean flip=true;
private Thread thread;
private boolean running;
private SurfaceHolder holder;
public BackgroundColors(Context context) {
super(context);
}
Inside this loop while running is true. is impossible to show dialogs ??
public void run() {
int r = 0;
while (running){
if (holder.getSurface().isValid()){
Canvas canvas = holder.lockCanvas();
if (r > 250)
r = 0;
r += 10;
if (r>30 && flip){
flip=false;
// *********************************
dialog.show();
// *********************************
// CRASH !!
}
try {
Thread.sleep(300);
}
catch(InterruptedException e) {
e.printStackTrace();
}
canvas.drawARGB(255, r, 255, 255);
holder.unlockCanvasAndPost(canvas);
}
}
}
public void start() {
running = true;
thread = new Thread(this);
holder = this.getHolder();
thread.start();
}
public void stop() {
running = false;
boolean retry = true;
while (retry){
try {
thread.join();
retry = false;
}
catch(InterruptedException e) {
retry = true;
}
}
}
public boolean onTouchEvent(MotionEvent e){
dialog.show();
return false;
}
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld){
super.onSizeChanged(xNew, yNew, xOld, yOld);
grand = xNew;
step =grand/15;
}
}
public void onCreate(Bundle b) {
super.onCreate(b);
view = new BackgroundColors(this);
this.setContentView(view);
builder = new AlertDialog.Builder(this);
builder.setMessage("ciao");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d("Basic", "It worked");
}
});
dialog = builder.create();
}
public void onPause(){
super.onPause();
view.stop();
}
public void onResume(){
super.onResume();
view.start();
}
}
you cann't show dialog in thread.you should use handler for this.create a handler in main thread and send it to your thread and instead of dialog.show() in your thread you should send message to handler and in handleMessage method of handler write dialog.show().
example:
Handler handler = new Handler(){
#Override
public void handleMessage(Message msg) {
switch(msg.what) {
case 1:
dialog.show();
break;
}}};
and send message in thread:
handler.sendEmptyMessage(1);

Resources