-1

I have below code configure to generate notification :

Intent intent = new Intent(this, HomeActivity.class);
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);


        intent.putExtra("IsNotification",true);

        int mNotificationId= Pref.getValue(this, Constants.NOTIFICATION_ID,1);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,mNotificationId /* Request code */, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        Bitmap mBitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(mBitmap)
                .setContentTitle("App Notification").setColor(getResources().getColor(R.color.orange))
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(mNotificationId /* ID of notification */, notificationBuilder.build());

        mNotificationId++;

        Pref.setValue(this,Constants.NOTIFICATION_ID,mNotificationId);

Now in Manifest i have

<activity
            android:name=".HomeActivity"
            android:label="@string/title_activity_home"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustPan"
            android:launchMode="singleTop"
            />

Now in OnCreate and onNewIntent on both i am checking that it's bundle have some notification value

Like below code commit fragment from OnCreate or onNewIntent depend on Activity Instance :

private void initNotification(){

        if(mNotitficationFragment==null){

            mNotitficationFragment= NotitficationFragment.newInstance();
        }

        if(!mNotitficationFragment.isAdded()){

            mFragManager.beginTransaction().replace(R.id.frame_home_container,mNotitficationFragment).commit();

        }



    }

So now my issue is when onCreateView of NotificationFragment called it's giving `getActivity() as null .

NOTE

For Fragment i am using support-v4:23.1.1 for Fragment .

Herry
  • 7,037
  • 7
  • 50
  • 80
  • What if you got context in `Frgament onAttach(...)`? As [this](http://stackoverflow.com/questions/6215239/getactivity-returns-null-in-fragment-function) post said – M D Apr 14 '16 at 06:12
  • @MD i need to check that – Herry Apr 14 '16 at 06:23
  • @MD Also it's shown as deprecated for onAttach(Activity mActivity) – Herry Apr 14 '16 at 06:25
  • You have to wait for the fragment to be attached to the activity, do it in onAttach or later. – Nanoc Apr 14 '16 at 06:27

1 Answers1

0

Try to do the related processing on or after onActivityCreated or in onAttach. For the onAttach deprecated warning, check this answer.

Community
  • 1
  • 1
jaibatrik
  • 6,770
  • 9
  • 33
  • 62