Paho Mqtt client does not fetch data after third or fourth times related fragment opens Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Why does startActivity work in one method and fail in another?How to delete a Row from the DB in androidNo view found for id 0x7f090005get around of error 'can not resolve symbol'Automatically download image from serverError TimePickerDialog. Here is the code:Search Firestore query don't show data in RecycleViewSend data from Adapter to Fragment and get it in fragmentCloud Firestore Security Rules permission

Is the Standard Deduction better than Itemized when both are the same amount?

What causes the direction of lightning flashes?

Extracting terms with certain heads in a function

Circuit to "zoom in" on mV fluctuations of a DC signal?

How to tell that you are a giant?

How do I stop a creek from eroding my steep embankment?

Where are Serre’s lectures at Collège de France to be found?

An adverb for when you're not exaggerating

What is homebrew?

Generate an RGB colour grid

Why wasn't DOSKEY integrated with COMMAND.COM?

Dating a Former Employee

Why aren't air breathing engines used as small first stages

Why are both D and D# fitting into my E minor key?

Is there a kind of relay only consumes power when switching?

When a candle burns, why does the top of wick glow if bottom of flame is hottest?

What would be the ideal power source for a cybernetic eye?

Is it fair for a professor to grade us on the possession of past papers?

2001: A Space Odyssey's use of the song "Daisy Bell" (Bicycle Built for Two); life imitates art or vice-versa?

What does this Jacques Hadamard quote mean?

Using et al. for a last / senior author rather than for a first author

How do I find out the mythology and history of my Fortress?

How come Sam didn't become Lord of Horn Hill?

How do I make this wiring inside cabinet safer? (Pic)



Paho Mqtt client does not fetch data after third or fourth times related fragment opens



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Why does startActivity work in one method and fail in another?How to delete a Row from the DB in androidNo view found for id 0x7f090005get around of error 'can not resolve symbol'Automatically download image from serverError TimePickerDialog. Here is the code:Search Firestore query don't show data in RecycleViewSend data from Adapter to Fragment and get it in fragmentCloud Firestore Security Rules permission



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am using mqtt paho android client and fetching data in a fragment without problem. After navigating to another fragments 3. or 4. times from that fragment and being back to that fragment, data does not come from cloud server. How can I handle this problem? I do not make any operation on life cycle functions like onResume onPause etc.



public class PFragment extends Fragment 

View view;
private final MemoryPersistence persistence = new MemoryPersistence();
MqttAndroidClient client;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
view = inflater.inflate(R.layout.fragment_plant, container, false);
ButterKnife.bind(this, view);
setHasOptionsMenu(true);
connect();
openGame();
openStats();

return view;


private void openGame()
game.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
getFragmentManager().beginTransaction()
.replace(R.id.frame, new GameFragment(), "fragment_settings")
.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack("game")
.commit();

);


private void openStats()
stats.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
getFragmentManager().beginTransaction()
.replace(R.id.frame, new StatsFragment(), "fragment_settings")
.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack("stats")
.commit();

);


private void disconnect()
try
client.disconnect();
catch (Exception ex)
ex.printStackTrace();


client.unregisterResources();


public void connect()

String clientId = MqttAsyncClient.generateClientId();
client = new MqttAndroidClient(getActivity(), "mqtturl",
clientId, persistence);


MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
mqttConnectOptions.setUserName("xx");
mqttConnectOptions.setPassword("xx".toCharArray());
mqttConnectOptions.setCleanSession(true);



try
client.connect(mqttConnectOptions, null, new IMqttActionListener()
@Override
public void onSuccess(IMqttToken asyncActionToken)
Log.i("a", "Connection Success!");
try

client.subscribe("x", 0);

water.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
try
if (a.getText().toString().equalsIgnoreCase("push"))
client.publish("a", new MqttMessage("1".getBytes()));

else if (water_me_text.getText().toString().equalsIgnoreCase("pull"))

client.publish("a", new MqttMessage("0".getBytes()));



catch (MqttException e)
e.printStackTrace();


);

catch (MqttException ex)




@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception)


);
catch (MqttException ex)
L



client.setCallback(new MqttCallback()
@Override
public void connectionLost(Throwable cause)




@Override
public void messageArrived(String topic, MqttMessage message) throws Exception
if (topic.equals("x"))



checkPlantStatus("Amaryllis");


@Override
public void deliveryComplete(IMqttDeliveryToken token)

);

















share|improve this question



















  • 1





    Please always share your code.

    – Boken
    Mar 22 at 9:38






  • 1





    Seems that you are creating the connection every time the view is created which in turn is creating duplicate connections and finally your MQTT connection is giving up on it. Kindly share the code so that I can look further into it.

    – Huzefa Gadi
    Mar 22 at 10:49











  • @HuzefaGadi added

    – Davis
    Mar 22 at 14:40

















0















I am using mqtt paho android client and fetching data in a fragment without problem. After navigating to another fragments 3. or 4. times from that fragment and being back to that fragment, data does not come from cloud server. How can I handle this problem? I do not make any operation on life cycle functions like onResume onPause etc.



public class PFragment extends Fragment 

View view;
private final MemoryPersistence persistence = new MemoryPersistence();
MqttAndroidClient client;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
view = inflater.inflate(R.layout.fragment_plant, container, false);
ButterKnife.bind(this, view);
setHasOptionsMenu(true);
connect();
openGame();
openStats();

return view;


private void openGame()
game.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
getFragmentManager().beginTransaction()
.replace(R.id.frame, new GameFragment(), "fragment_settings")
.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack("game")
.commit();

);


private void openStats()
stats.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
getFragmentManager().beginTransaction()
.replace(R.id.frame, new StatsFragment(), "fragment_settings")
.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack("stats")
.commit();

);


private void disconnect()
try
client.disconnect();
catch (Exception ex)
ex.printStackTrace();


client.unregisterResources();


public void connect()

String clientId = MqttAsyncClient.generateClientId();
client = new MqttAndroidClient(getActivity(), "mqtturl",
clientId, persistence);


MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
mqttConnectOptions.setUserName("xx");
mqttConnectOptions.setPassword("xx".toCharArray());
mqttConnectOptions.setCleanSession(true);



try
client.connect(mqttConnectOptions, null, new IMqttActionListener()
@Override
public void onSuccess(IMqttToken asyncActionToken)
Log.i("a", "Connection Success!");
try

client.subscribe("x", 0);

water.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
try
if (a.getText().toString().equalsIgnoreCase("push"))
client.publish("a", new MqttMessage("1".getBytes()));

else if (water_me_text.getText().toString().equalsIgnoreCase("pull"))

client.publish("a", new MqttMessage("0".getBytes()));



catch (MqttException e)
e.printStackTrace();


);

catch (MqttException ex)




@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception)


);
catch (MqttException ex)
L



client.setCallback(new MqttCallback()
@Override
public void connectionLost(Throwable cause)




@Override
public void messageArrived(String topic, MqttMessage message) throws Exception
if (topic.equals("x"))



checkPlantStatus("Amaryllis");


@Override
public void deliveryComplete(IMqttDeliveryToken token)

);

















share|improve this question



















  • 1





    Please always share your code.

    – Boken
    Mar 22 at 9:38






  • 1





    Seems that you are creating the connection every time the view is created which in turn is creating duplicate connections and finally your MQTT connection is giving up on it. Kindly share the code so that I can look further into it.

    – Huzefa Gadi
    Mar 22 at 10:49











  • @HuzefaGadi added

    – Davis
    Mar 22 at 14:40













0












0








0








I am using mqtt paho android client and fetching data in a fragment without problem. After navigating to another fragments 3. or 4. times from that fragment and being back to that fragment, data does not come from cloud server. How can I handle this problem? I do not make any operation on life cycle functions like onResume onPause etc.



public class PFragment extends Fragment 

View view;
private final MemoryPersistence persistence = new MemoryPersistence();
MqttAndroidClient client;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
view = inflater.inflate(R.layout.fragment_plant, container, false);
ButterKnife.bind(this, view);
setHasOptionsMenu(true);
connect();
openGame();
openStats();

return view;


private void openGame()
game.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
getFragmentManager().beginTransaction()
.replace(R.id.frame, new GameFragment(), "fragment_settings")
.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack("game")
.commit();

);


private void openStats()
stats.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
getFragmentManager().beginTransaction()
.replace(R.id.frame, new StatsFragment(), "fragment_settings")
.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack("stats")
.commit();

);


private void disconnect()
try
client.disconnect();
catch (Exception ex)
ex.printStackTrace();


client.unregisterResources();


public void connect()

String clientId = MqttAsyncClient.generateClientId();
client = new MqttAndroidClient(getActivity(), "mqtturl",
clientId, persistence);


MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
mqttConnectOptions.setUserName("xx");
mqttConnectOptions.setPassword("xx".toCharArray());
mqttConnectOptions.setCleanSession(true);



try
client.connect(mqttConnectOptions, null, new IMqttActionListener()
@Override
public void onSuccess(IMqttToken asyncActionToken)
Log.i("a", "Connection Success!");
try

client.subscribe("x", 0);

water.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
try
if (a.getText().toString().equalsIgnoreCase("push"))
client.publish("a", new MqttMessage("1".getBytes()));

else if (water_me_text.getText().toString().equalsIgnoreCase("pull"))

client.publish("a", new MqttMessage("0".getBytes()));



catch (MqttException e)
e.printStackTrace();


);

catch (MqttException ex)




@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception)


);
catch (MqttException ex)
L



client.setCallback(new MqttCallback()
@Override
public void connectionLost(Throwable cause)




@Override
public void messageArrived(String topic, MqttMessage message) throws Exception
if (topic.equals("x"))



checkPlantStatus("Amaryllis");


@Override
public void deliveryComplete(IMqttDeliveryToken token)

);

















share|improve this question
















I am using mqtt paho android client and fetching data in a fragment without problem. After navigating to another fragments 3. or 4. times from that fragment and being back to that fragment, data does not come from cloud server. How can I handle this problem? I do not make any operation on life cycle functions like onResume onPause etc.



public class PFragment extends Fragment 

View view;
private final MemoryPersistence persistence = new MemoryPersistence();
MqttAndroidClient client;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
view = inflater.inflate(R.layout.fragment_plant, container, false);
ButterKnife.bind(this, view);
setHasOptionsMenu(true);
connect();
openGame();
openStats();

return view;


private void openGame()
game.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
getFragmentManager().beginTransaction()
.replace(R.id.frame, new GameFragment(), "fragment_settings")
.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack("game")
.commit();

);


private void openStats()
stats.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
getFragmentManager().beginTransaction()
.replace(R.id.frame, new StatsFragment(), "fragment_settings")
.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack("stats")
.commit();

);


private void disconnect()
try
client.disconnect();
catch (Exception ex)
ex.printStackTrace();


client.unregisterResources();


public void connect()

String clientId = MqttAsyncClient.generateClientId();
client = new MqttAndroidClient(getActivity(), "mqtturl",
clientId, persistence);


MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
mqttConnectOptions.setUserName("xx");
mqttConnectOptions.setPassword("xx".toCharArray());
mqttConnectOptions.setCleanSession(true);



try
client.connect(mqttConnectOptions, null, new IMqttActionListener()
@Override
public void onSuccess(IMqttToken asyncActionToken)
Log.i("a", "Connection Success!");
try

client.subscribe("x", 0);

water.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
try
if (a.getText().toString().equalsIgnoreCase("push"))
client.publish("a", new MqttMessage("1".getBytes()));

else if (water_me_text.getText().toString().equalsIgnoreCase("pull"))

client.publish("a", new MqttMessage("0".getBytes()));



catch (MqttException e)
e.printStackTrace();


);

catch (MqttException ex)




@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception)


);
catch (MqttException ex)
L



client.setCallback(new MqttCallback()
@Override
public void connectionLost(Throwable cause)




@Override
public void messageArrived(String topic, MqttMessage message) throws Exception
if (topic.equals("x"))



checkPlantStatus("Amaryllis");


@Override
public void deliveryComplete(IMqttDeliveryToken token)

);














android android-fragments mqtt






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 14:40







Davis

















asked Mar 22 at 9:37









DavisDavis

367




367







  • 1





    Please always share your code.

    – Boken
    Mar 22 at 9:38






  • 1





    Seems that you are creating the connection every time the view is created which in turn is creating duplicate connections and finally your MQTT connection is giving up on it. Kindly share the code so that I can look further into it.

    – Huzefa Gadi
    Mar 22 at 10:49











  • @HuzefaGadi added

    – Davis
    Mar 22 at 14:40












  • 1





    Please always share your code.

    – Boken
    Mar 22 at 9:38






  • 1





    Seems that you are creating the connection every time the view is created which in turn is creating duplicate connections and finally your MQTT connection is giving up on it. Kindly share the code so that I can look further into it.

    – Huzefa Gadi
    Mar 22 at 10:49











  • @HuzefaGadi added

    – Davis
    Mar 22 at 14:40







1




1





Please always share your code.

– Boken
Mar 22 at 9:38





Please always share your code.

– Boken
Mar 22 at 9:38




1




1





Seems that you are creating the connection every time the view is created which in turn is creating duplicate connections and finally your MQTT connection is giving up on it. Kindly share the code so that I can look further into it.

– Huzefa Gadi
Mar 22 at 10:49





Seems that you are creating the connection every time the view is created which in turn is creating duplicate connections and finally your MQTT connection is giving up on it. Kindly share the code so that I can look further into it.

– Huzefa Gadi
Mar 22 at 10:49













@HuzefaGadi added

– Davis
Mar 22 at 14:40





@HuzefaGadi added

– Davis
Mar 22 at 14:40












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55296683%2fpaho-mqtt-client-does-not-fetch-data-after-third-or-fourth-times-related-fragmen%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55296683%2fpaho-mqtt-client-does-not-fetch-data-after-third-or-fourth-times-related-fragmen%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript