I am using a Fragment and within my Fragment, I am starting a new Intent to access the device's settings. When I select the 'Back' button from the Android device, my bindings do not reflect in my View. The ViewModel properties are correct when I debug but the View doesn't reflect live data updates anymore. This only happens when I open the Intent
I am opening the Intent here:
private View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
};
I am using the ObservableFields in my ViewModel so I think it's working correctly as I mentioned before - the data is correct on the debugger, just not reflected in the View if I go into the Intent. The View will reflect any changes if I do not go into the Intent. Do I need to Override the onResume method?
As the doc says,
the fields should be declared final in your data model because bindings only detect changes in the field's value, not of the field itself
Please check this statement. If if didn't fix your problem, can you post some additionnal code as your data model class, and how you use it in your fragment?
Hope it helps.
Related
I have a problem with my current project in android studio.
I am trying to write some code, which creates new TextViews and ImageViews for a new activity.
This code and the new activity should start if a button is clicked.
To add the new Viewers, I try to access the existing LinearLayout in the xml-file of the new activity. But it always says that I try to "invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference"
So my question is how can I give the Linear Layout some context?
Here is my onClick-method. The parseXML()-method creates the new Viewers by parsing an XML-file.
public void onClick(View view) {
linearLayout= findViewById(R.id.linearLayoutArtikel);
parseXML();
Intent intent= new Intent(this, ArtikelActivity.class);
startActivity(intent);
}
EDIT
Thanks for your suggestions, moving the code to the new Activity solved the problem!
LinearLayout is part of ArtikelActivity if you are trying to access it from anyother activity then you will get that message. So move your code to ArtikelActivity. This is gonna work surely.
I'm curious as to whether an intent is unique to a set activity or not.
For example, my current usage of an intent is to give it some data from Activity 1 and pass it on to Activity 2.
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("id", id.getText().toString());
intent.putExtra("weight", weight.getText().toString());
intent.putExtra("zipTo", zipTo.getText().toString());
intent.putExtra("locationId", locationID.getSelectedItem().toString());
intent.putExtra("imageNum", 1);
startActivity(intent);
Now, in MainActivity I can use the following code to retrieve the data passed from the previous activity.
Bundle bundle = getIntent().getExtras();
id = bundle.getString("id");
weight = bundle.getString("weight");
locationID = bundle.getString("locationID");
zipTo = bundle.getString("zipTo");
After doing some work in MainActivity, I need to return to the previous activity that sent us to MainActivity (and I need the data that was originally sent to return).
Do I need to redefine an intent, and do "intent.putExtra" for each value again in MainActivity before sending it to the first Activity? Or is the intent global, and once defined in one activity I can use it in the others with getIntent().getExtras()?
Intents are not global. You only have access to the bundle in the activity you started with the intent passed.
You should use startActivityForResult(intent). This provides the behavior youre looking for.
Getting a result from an activity
The idea is you start the new intent. In this case 'MainActivity'. Once you are done with your logic in MainActivity you use the strategy above to pass a result back to the activity that started MainActivity.
information passed through intents are not global from one activity to another, what is global is shared preferences and information that is metadata which is stored in manifest.xml.
In android Studio, I want when i click on button , next activity/fragment should come from right side and present activity sholud gone left.I implimented its working on Activity but not on adapters is showing error.
holder.questions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(DoctorsProfile.this,Questions.class);
i.putExtra("DOCTOR_ID",doctor_id);
startActivity(i);
overridePendingTransition( R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}
});
overridePendingTransition is working on Activity but not working on Adapters of Recyclerview and Listview, Please tell any other option. I want when i click on recyclerview item next Activity should navigate or come from right side by using overridePendingTransition.
Fragment fragment = Fragment.newInstance();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.fragment_slide_left_enter,
R.anim.fragment_slide_left_exit, R.anim.fragment_slide_right_enter,
R.anim.fragment_slide_right_exit);
Utils.addFragmentToActivity(fragmentTransaction, Fragment, R.id
.content_frame);
This tip features how to change Android’s default animation when switching between Activities.
The code to change the animation between two Activities is very simple: just call the overridePendingTransition() from the current Activity, after starting a new Intent. This method is available from Android version 2.0 (API level 5), and it takes two parameters, that are used to define the enter and exit animations of your current Activity.
Here’s an example:
//Calls a new Activity
startActivity(new Intent(this, NewActivity.class));
//Set the transition -> method available from Android 2.0 and beyond
overridePendingTransition(R.anim.slide_in_right_up, R.anim.slide_out_right_up);
These two parameters are resource IDs for animations defined with XML files (one for each animation). These files have to be placed inside the app’s res/anim folder. Examples of these files can be found at the Android API demo, inside the anim folder.
for example code visit http://www.christianpeeters.com/android-tutorials/tutorial-activity-slide-animation/#more-483
Change like this code you must be passing activity as context in adapter
holder.questions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(DoctorsProfile.this,Questions.class);
i.putExtra("DOCTOR_ID",doctor_id);
Activity activity = (Activity) context;
activity.startActivity(i);
activity.overridePendingTransition(R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}
});
Note : Context is the Base Object of Activity
Update :
I have checked the accepted answer but hope you understand that it will be called everytime when your activity get launched and thats not supposed to be best practice. I am suggesting better approach if you want to follow the accepted answer .
Alternative :
Pass one parameter in bundle to new activity to make sure that transition coming from that specific adapter so double transation should not happen when you are coming rom any other activity also.
There is a easy way to do this. just put overridePendingTransition on your OnCreate method of next Activity/Fragment.So that when next Activity will come it will come according to your choice.Need not add overridePendingTransition on adapters.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ask_question);
overridePendingTransition( R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}
I have a ListView of database items in my main activity, in my main activity i have a method that populates the list view from my database that runs on the oncreate of the main method.
from my main activity the user can go into the menu and click on something called "change view" which is an activity that lets the user change the search criteria for the database, I then have a method that populates the listview from this search criteria.
After the user clicks on the change view button within this activity i want the application to then go back to the main activity and to display this new custom list view, my first thought was to call this method in the on resume, but that doesn't work for the obvious reasons, would appreciate any direction.
you can use startActivityForResult from your main activity or the activity with the list to call the change view activity and then pass the selected option back to the calling activity and use the onActivityResult to read the data returned
and then populate the listview based on it
this link will give you a starting point.or you can use the official reference
You can use custom broadcast receiver for that like below
When you click on the button write this code
Intent i = new Intent("android.intent.action.MAIN")
this.sendBroadcast(i);
finish()//finish your current button activity if this is an activity
Now in onResume of listview activity
IntentFilter intentFilter = new IntentFilter(
"android.intent.action.MAIN");
mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Update you listview
}
};
//registering our receiver
this.registerReceiver(mReceiver, intentFilter);
And in onPause you must unregister the receiver like below code
this.unregisterReceiver(this.mReceiver);
As we know the main activity in Android, Eclipse is called MainActivity.java
Actually I have two activites, the second one is: Page2Activity.java
And I have a Page2.xml too for the layout.
I would like to know how can I switch to Page2Activity.java when pushing a button? Because only Page2.xml shows up, and when I click on a button to play a sound nothing happens on the second page.
MainActivity.java
...
bpage2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.page2);
}
});
...
When I push this button, page2.xml shows up, but it contains sound from Page2Activity.java and when I hit a button the sounds won't play. Could you please tell me how can I load Page2Activity.java with layout page 2?
Regards,
Henrik
You have to fire an event on click action for the button and start page2 Activity on it's listener.
bpage2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, Page2Activity.class);
startActivty(i);
});
Why don't you start the other activity Page2Activity when you click on yout bpage2-Button?
Put this in your listener instead of setContentView(R.layout.page2); :
Intent intent = new Intent(this, Page2Activity.class);
startActivity(intent);
Calling setContentView() sets the layout of your activity - that's why you see the other layout after clicking the button. So what you do is just switching the layout but the logic (e.g. attached listeners) is missing and needs to be implemented. That's why the sound doesn't play. The layout coexists with the logic but it's independant from the logic. Including a xml-layout doesn't mean you include the logic too.
Check out the Fragments http://developer.android.com/guide/components/fragments.html.
In addition take a look on this introduction on how to start an activity from another one: http://developer.android.com/training/basics/firstapp/starting-activity.html .
Maybe you should even start here http://developer.android.com/guide/components/activities.html :)
Good luck.