Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8221745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:02:21+00:00 2026-06-07T14:02:21+00:00

I am facing a problem, that I created a class Controller it is singleton

  • 0

I am facing a problem, that I created a class Controller it is singleton but its object is recreating when I access in different activity of same application,

Main_Activity is my launching activity

public class Main_Activity extends Activity{
       private Controller simpleController;
       protected void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
                 setContentView(R.layout.main);
                 simpleController = Controller.getInstance(this);
       }
}

This is my Controller it is singleton, in it I am setting alarm which is of 10sec from now and my MyMainLocalReciever receives that alarm and notify using notification.

public class Controller {
       private MediaPlayer mp;
       public Context context;
       private static Controller instance;

       public static Controller getInstance(Context context) {
              if (instance == null) {
                    instance = new Controller(context);
              }
              return instance;
       }

      private Controller(Context context) {
            Log.d("TAG", "Creating Controller object");
            mp = null;
            this.context = context;
            setAlarm(10);
        }

     public void setAlarm(int position) {
        Intent intent = new Intent(context, MyMainLocalReciever.class);
        intent.putExtra("alarm_id", "" + position);
        PendingIntent sender = PendingIntent.getBroadcast(context,
                position, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        // Get the AlarmManager service
        AlarmManager am = (AlarmManager) context
                .getSystemService(Activity.ALARM_SERVICE);
        am.cancel(sender);
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                + (position*1000), sender);
    }

}

This is my receiver MyMainLocalReciever it notify and I am binding an intent which starts an activity called NotificationDialog

public class MyMainLocalReciever extends BroadcastReceiver {
private NotificationManager notificationManager;
private int alarmId = 0;

@Override
public void onReceive(Context context, Intent intent) {
    if (notificationManager == null) {
        notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
    }
    Bundle bundle = intent.getExtras();

    String alarm_Id = bundle.getString("alarm_id");

        try {
        alarmId = Integer.parseInt(alarm_Id);
    } catch (Exception e) {
        Log.d("Exception", "exception in converting");
    }

    Controller myC = Controller.getInstance(context);
    if ((myC.getMp() != null)) {
        myC.getMp().stop();
        myC.setMp(null);
    }
    if (myC.getMp() == null) {

            myC.setMp(MediaPlayer.create(context , R.id.mpFile));
            myC.getMp().start();
    }

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setTicker("Its Ticker")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Its Title")
            .setContentText("Its Context")
            .setAutoCancel(true)
            .setContentIntent(
                    PendingIntent.getActivity(context, 0, new Intent(context,
                            NotificationDialog.class)
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                                    | Intent.FLAG_ACTIVITY_CLEAR_TASK), 0));

    notificationManager.notify("interstitial_tag", alarmId,
            builder.getNotification());

}

}

Till now(before NotificationDialog) code is working perfect MediaPlayer object which is in Controller class is working fine too, but when I access my singleton Controller here in NotificationDialog, it is creating new object of Controller, it should not do that, it should retain that Controller object which is singleton.

public class NotificationDialog extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notification_dialog);

}

public void onViewContent(View v) { //this method is invoked when I click on a button binded in xml file

    Controller myC = Controller.getInstance(getApplicationContext());

    if (myC.getMp() != null) {
        myC.getMp().stop();
        myC.setMp(null);
    }
    finish();
}

}

Kindly help me regarding this, I will appreciate your help.
Regards

EDIT:
Here is my Manifest

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Main_Activity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="test.SettingsActivity"
        android:label="@string/app_name" />
    <activity
        android:name="test.NotificationDialog"
        android:label="@string/app_name" />
    <service android:name="test.MyService" >
    </service>

    <receiver
        android:name="test.MyMainLocalReciever"
        android:process=":remote" />
</application>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:02:23+00:00Added an answer on June 7, 2026 at 2:02 pm

    Your process is getting killed by Android when it is idle in the background. Android will kill off your process if there are no active components (Activities, Services, etc.) or when it needs the memory (even if you have active components).

    When the user uses your notification, Android creates a new process for you. That is why the Singleton is gone and needs to get recreated.

    EDIT:

    After you posted your manifest I immediately saw the problem. This is it:

    <receiver
        android:name="test.MyMainLocalReciever"
        android:process=":remote" />
    

    Your process isn’t getting killed. Your BroadcastReceiver is running in another separate process. In that process, the singleton hasn’t been set up yet.

    Remove android:process=":remote" from your <receiver> tag in the manifest.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm facing a problem that drives me crazy, I created a java application using
Im currently facing the problem that when i try to set focus on some
I'm facing a problem that I can summarize as it follows: I have a
I'm facing a problem that seems to have no straighforward solution. I'm using java.util.Map
I am facing the problem that I cannot properly map my foreign key table
Here i am facing a problem that , i configured the hibernate sessionfactory in
I'm working with Eclipse and ClearCase and we're facing the problem that there's no
I am facing problem with iframe that too in chrome. How to make IFRAME
I am facing a design problem that I cannot figure out at all. I
So here is the problem that I am facing: I have an application that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.