I checked a string variable from web service every 15 min, if returns "0", then new layout open with new class. I want that if returns me different from "0" again main layout shown again. When I press back it shows whitout background music but I want it whole mainactivity and layout automaticly.
intent_second= new Intent(this, second.class);
if (str.equals("0")) {
startActivity(intent_second);
} else {
//Can I do something here for what I want?
qst.setText(str);
}
this is my code for showing second screen, what Can I do for my request with which class?
Can I do that from mainactivity (calling itself) or how can I send this variable to second class and how can I listen it and start again mainactivity?
if flag == 0 then start second activity and on second activity if flag == 1 then call finish() from second activity
onCreate() {
intent_second= new Intent(this, second.class);
if (str.equals("0")) {
startActivityForResult(intent_second,intent_code);
} else {
//Can I do something here for what I want?
qst.setText(str);
}
}
onActivityResult(Intent intent, code) {
if(code == intent_code) {
// update your main layout
String result=data.getStringExtra("result");
// here u get you data from second activity "result" is key and it should be used on second activity
}
}
Related
I am trying to get a QR code to be encoded based on data collected throughout the other fragments and sent to the MainActivity. Then, at the end, I want to call getAllData(), which returns all data in one string. It will do this when a FloatingActionButton is clicked (fabGenerate). Currently, it will load the fragment, and when you click on the fab, it will go into the correct case (R.id.fabGenerate) and run until "//error here". Also, I do NOT get an error message, due to the crash only occurring on the emulator.
String data;
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.fabGenerate:
Activity activity = getActivity();
if (activity != null && activity instanceof MainActivity) {
data = ((MainActivity) activity).getAllData(); //error here
}
qrgEncoder = new QRGEncoder(data, null, QRGContents.Type.TEXT, ((MainActivity)getActivity()).getDimen());
break;
}
}
I have tried first having data within the qrgEncoder line, where it will only do
qrgEncoder = new QRGEncoder(((MainActivity) getActivity).getAllData(), null, QRGContents.Type.TEXT, ((MainActivity)getActivity()).getDimen());
This did not work, and would still crash ONLY when it was run, not built.
I have also tried using Toast.makeText's to print a line, which determined that the error is within this piece of code.
I'm stuck by a problem and I don't know how too resolve it.
I want to click on my item in my recycler view, and at this click the layout with the recylcer view will be closed, and my mainActivity will be refresh with the data of the item who i have click on.
you start new activity to get Data but here you start a normal intent you need to start Activity For Result like
startActivityForResult(intent,REQUEST_CODE);
or new Way
ActivityResultLauncher<Intent> result = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
#Override
public void onActivityResult(ActivityResult result) {
if(result.getResultCode() == RESULT_OK){
String data = result.getData().getStringExtra("Data");
// here your data come from next Activity
}
}
});
then call result by
result.launch(Intent);
and in other Activity activity
you need to call
setResult(RESULT_OK,Intent);
finish();
this intent hold your data and will return to previous activity with it
After 2 hours in the sauce I have resolve that and I'm very happy.
The problem with the setResult() and the finish() but if you apply this code
((StructureEnFeuActivity)context).setResult(RESULT_OK, intent);
((StructureEnFeuActivity)context).finish();
You will not have the probleme with "static".
So I'm happy.
And thank you for all.
Currently finishing up an app I have been working on but I'm kinda stuck on this last thing.
The app has 2 activities, one with buttons that are categories and the other shows the information according to the button pressed. For example if you click the button Fast food, it goes to the 2nd screen and displays info on that.
I'm trying to get a refresh button on the 2nd activity that will call a method in the 1st activity to refresh new information depending on the button pressed. The problem is that I don't know how to make it so the method keeps the same argument when called. What I mean is, if fast food was clicked, the refresh button would get new info that still relates to the fast food category.
Here's the relevant code in the Main activity:
public void yelpSearch(View view) {
switch (view.getId()) {
case R.id.button: buttoncategory = "Restaurant";
break;
case R.id.button2: buttoncategory = "Chinese restaurant";
break;
case R.id.button3: buttoncategory = "Fast Food";
break;
and this on the 2nd Activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
Refresh();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void Refresh() {
MainActivity mActivity = new MainActivity();
mActivity.yelpSearch();
}
I'm not sure what to put inside mActivity.yelpSearch();
I tried using (View view) but it'll say cannot resolve symbol view. And if I make a local variable for view, it'll say not initialized and I don't know what to set it as
Any help would be awesome, been googling on this for a while now and searched through tons of questions on here as well. I'm still new at this so bear with me
I think you want to pass data between activities?
First Activity(Send DATA):
String data="YourXXXdata"
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.putExtra("DATANAME", data);
startActivity(intent)
second Activity(Receive DATA):
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("DATANAME");
}
The data now is in String value
*you have to put in data the value you need depend you preview select button.
Make your yelpSearch method static
and call it like this from 2nd activity MainActivity.yelpSearch();
I have a progress dialog I am trying to show when a user clicks a button to launch a new activity. The spinner should be displayed on the current page until the other activity appears. ( The activity can take sometimes up to 4-5 seconds to launch and without the spinner it just shows a pressed button that looks frozen )
This is what I have, it's only working if I remove hideProgressDialog();, but then the spinner will still be there when I back to the previous activity, obviously.
What am I doing wrong ?
Progress Dialog :
public void showProgressDialog(Context context) {
if(this.progressDialog != null) {
this.progressDialog.dismiss();
this.progressDialog = null;
}
this.progressDialog = ProgressDialog.show(context, "", "Chargement en cours, veuillez patienter");
}
public void hideProgressDialog() {
if(this.progressDialog != null) {
this.progressDialog.dismiss();
this.progressDialog = null;
}
}
Function :
public void startActivity(Context context, Class<? extends Activity> activityClass) {
try {
showProgressDialog(context);
Intent intent = new Intent(this, activityClass);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
hideProgressDialog();
}
catch(Throwable e) {
e.printStackTrace();
}
}
Example of a button click where this calls the function to show the spinner :
#Override
public void onClick(View view) {
if(view.getId() == R.id.changeBannerButton) {
getBaseApplication().startActivity(this, BannerListActivity.class);
}...
Call hideProgressDialog() in the onResume() method. This way, if the user presses the back button, the onResume() method gets called and immediately hides the progress dialog.
Well First off you should use the new DialogFragment class with FragmentManager. Because showdialog() is deprecated from API level 8
Next you should use showdialog and removedialog for adding and removing the dialog.
And you should use the onCreateDialog to handle the dialog and the operations. Like start a new thread to run do the job when you are displaying the progressdialog.
Try to load data in a seperate thread on start of activity. But before start of that process show the progress dialog. Now once the process is done use runOnUI to hide the progress dialog..
Indicate the user that data is being loading
http://www.helloandroid.com/tutorials/using-threads-and-progressdialog
and this:
Android - using runOnUiThread to do UI changes from a thread
A navigation drawer with 3 Fragments, the third Fragment has a TextView with an on Click listener. Once it has been clicked a layout activity will open on the top which includes a ListView to allow the user to select/click on a specific Item, so later on this selected item info should be displayed on that TextView within the third fragment.
is there any method to pass data because I have used a class to pass data but the TextView wouldn't be refreshed with the sent data
This works as a design pattern to share arguments between the Activity and third fragment
--------------------------DataHolder Class---------------------------------
public class DataHolder {
private static DataHolder dataHolder = null;
private DataHolder() {
}
public static DataHolder getInstance() {
if (dataHolder == null)
{dataHolder = new DataHolder(); }
return dataHolder;
}
private String item;
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
}
If you find using startActivityForResult not sufficient in your case or using EventBus, you can overcome this by using SharedPreferences and Fragment/Activity lifecycle.
So once you start new Activity first Activity will go in onPause and with it all its Fragments will be put in onPause. When user clicks on one of the ListView items in your second Activity you can store the value inside SharedPreferences like:
PreferenceManager.getDefaultSharedPreferences(SecondActivity.this)
.edit().putString(key, selectedItemInfoHere).apply();
Then override inside your first Activity and in your third Fragment method onResume() and inside just make checking:
#Override
public void onResume() {
super.onResume();
String value = PreferenceManager.getDefaultSharedPreferences(getContext())
.getString(key, "");
if (value != null && !value.isEmpty()) {
//You have selected item value update TextView
}
}
Note that once you don't need this value you will need to remove it, because it will update your TextView every time when onResume is called. To remove value just call:
PreferenceManager.getDefaultSharedPreferences(getContext()).edit().remove(key);
If I understood you correctly, you have flow 3rd fragment --> activity which should update fragment which launched it. In this case, as for me, the most clean solution is from your opened activity call startActivityForResult method to call activity-host of your fragments and handle all what you need in overridden onActivityResult method. Than just call your fragment's updateTextView() or something like that.
On the other hand you can use this library to send messages between components, but you should be careful with usage and think about corner cases related to components lifecycle.
So, choose solution according your needs:)