How to call function containing intent (in Activity A) from Activity B - java

I am Calling a function(present in fragment) from activity when back button is pressed
public void onBackPressed() {
new Home().show(Home.home_list,app.this);
}
Home is fragment of app activity
In show function I am calling an intent
public void show(final ArrayList<tile_data> data, final Activity activity) {
startActivity(new Intent(activity, Chat_topic_layout_for_user_group.class)
}
In doing so it gives me error : java.lang.IllegalStateException: Fragment Home{5deabab} not attached to Activity
My conclusion is that app is using show as function so it doesn't know about home.
So my question is how to call an "intent" present in some function in some activity or fragment from other activity??

Looks like startActivity is not being called in the right scope. Try specifying the scope and calling it like this: activity.startActivity(new Intent(activity, Chat_topic_layout_for_user_group.class)

Why don't you move the Show() function to the Activity? And for the titles create a getter function in the Home fragment, getTitles(). Now, when the event occurs trigger the Show() function directly in the activity passing (*HomeFragment*.getTitle() , *Activity*.this). Are your titles static? Because you are directly calling new Home(). If that's the case there won't be any issue in calling Show() function in the activity. You can explain your requirements in detail if this isn't up to par.

public void show(final ArrayList<tile_data> data, final Activity activity) {
activity.startActivity(new Intent(activity, Chat_topic_layout_for_user_group.class));
}

Related

Sending a recorded audio file from one activity to another

I am currently recording a message and initialising the objects in one fragment. I'd like to pass it to my main activity. However, I can pass an image between fragments using this:
public void captureImage() {
mCamera.captureImage(new CameraKitEventCallback<CameraKitImage>() {
#Override
public void callback(CameraKitImage cameraKitImage) {
((MainActivity) getActivity()).setBitmapToSend(cameraKitImage.getBitmap());
((MainActivity) getActivity()).openDisplayImageFragment();
}
});
}
I have initialised a media recorder within the class. Is there any way to do this?
If the Main Activity is hosting your fragment, then you can send the data from fragment to its parent Activity by - Interface callbacks or creating a public method in your Activity.
Since you can always get the reference to the parent Activity using getActivity(), you can cast it and pass values to the public method. Eg:
myActivity=(MainActivity) getActivity ();
myActivity.receiveAudio(<"Pass the data as object or in byte[]">);
Sending data from a fragment to another activity require to add Intent extras. Eg:
myIntent.putExtra("myKey",<"Place your object or byte array">);
startActivity(myActivity);

How to finish the main activity from other Activity

Hello how do you finish Main activity
Assume that there are 3 Activities and 1 Fragment
LoginActivity , MainActivity, infoFrgMent, ChangePwdActivity.
The scenario is when I loggedin in LoginActivitythen MainActivity will show up LoginActivity will finish() then i will go to my info which is 'infoFrgMent' then i want to change my password after i changed my password.
LoginActivity will shows up Again to relogin but whenever i try to press back MainActivity shows up and didn't finished.
You need to remove previous activities form stack
setFlags to intent from MainActivity to LoginActivity
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
refer : https://developer.android.com/guide/components/activities/tasks-and-back-stack.html
What you need is to add the Intent.FLAG_CLEAR_TOP. This flag makes sure that all activities above the targeted activity in the stack are finished and that one is shown.
Another thing that you need is the SINGLE_TOP flag. With this one you prevent Android from creating a new activity if there is one already created in the stack.
Just be wary that if the activity was already created, the intent with these flags will be delivered in the method called onNewIntent(intent) (you need to overload it to handle it) in the target activity.
Then in onNewIntent you have a method called restart or something that will call finish() and launch a new intent toward itself, or have a repopulate() method that will set the new data. I prefer the second approach, it is less expensive and you can always extract the onCreate logic into a separate method that you can call for populate.
To finish another Activity you have to create a static method to finish this Like here:
MainActivity.java
private static MainActivity activity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
activity = this;
}
public static void finishThis()
{
try
{
if (activity != null)
{
activity.finish();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
And call it like this:
AnotherActivity.java
MainActivity.finishThis();
That's it

Adapter values is not updated from another activity

I am stuck with a problem where I need to update the value of a adapter variable in another activity and coming back to first activity should also get that updated value.
my current flow is like. I am staring the BarDetailsActivity and passing the modal with intent from inside the adapter class like:
Intent barDetailIntent = new Intent(getApplicationContext(), BarDetailActivity.class);
barDetailIntent.putExtra("isfav", barsList.get(position));
barDetailIntent.putParcelableArrayListExtra("barlist",barsList);
mContext.startActivity(barDetailIntent);
Then on another activity I am getting that model from intent and changing its variable values as:
gbar = in.getParcelableExtra("isfav");
blist= in.getParcelableArrayListExtra("barlist");
if (gbar.getmFavourite()) {
gbar.setmFavourite(false);
} else {
gbar.setmFavourite(true);
}
Now on going back to my main Activity Value for "gbar.setmFavourite" is not updated on onresume of the MainActivity.
#Override
protected void onResume() {
super.onResume();
if(mAdapter != null){
mAdapter.notifyDataSetChanged(); // here the adapter value is not updated.
}
}
Please help me on this.
Use Event Bus to handle the problem.
Register your First Activity to listen the Event and override
onEvent method.
Fire the stickyIntent with updated dataset from SecondActivity.
In the onEvent method of the FirstActivity call notifyDataSetChanged
with the updated dataset.

Retaining variable value when stopping android activity

I've got two activities, A and B. Activity B can be opened by pressing a button in Activity A.
In activity B I have an integer variable which I would like to keep for when I return to activity B from A. My problem is when I press the back button to go from B to A the activity is destroyed.
I have overwritten the onBackPressed method to:
#Override
public void onBackPressed(){
Intent i = new Intent(this, Game.class);
startActivity(i);
}
I can see from my logs that activity B is in the state onStop() after back button is pressed, however, onRestart() is not being called so the activity must be getting killed for memory reasons.
I have read answers to other posts suggesting I use onSaveInstanceState() but when I try to access the bundle in onCreate() the bundle is null. Method onRestoreInstanceState() does not get called.
protected void onSaveInstanceState(Bundle savedInstanceState){
Log.i(LOG, "instance saving");
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("score", userScore);
}
I have also tried SharedPreferences but this is not useful because I do not want my data to persist when the activity/application is intentionally destroyed.
I think your problem is in understanding the whole Task ecosystem. When you press back button you pop out your activity from the Task, because of that it is destroyed and onDestroyed() is called. To sum-up I think you are just getting every time a brand new activity. onSaveInstanceState() isn't called because activity is killed by user, not by the OS.
Take a deeper look at this developer tutorial.
Also I think those two must be helpful : me and me!
you can store variables in the app class https://developer.android.com/reference/android/app/Application.html or you can make your own singleton class for this
https://www.tutorialspoint.com/design_pattern/singleton_pattern.htm
Starting a Activity - A on onBackPressed will definitely kill the current Activity - B. Instead of starting Activity again just call onBackPressed in Activity - B and add a stage called onResume() which is called when you resume back to Activity B from A
Remove this:
#Override
public void onBackPressed(){
Intent i = new Intent(this, Game.class);
startActivity(i);
}
With this:
#Override
public void onBackPressed(){
super.onBackPressed();
}
When you coming back from A to B, in B #Override stage onResume() and in this you can save the value while coming back from Activity A.
Add this in Activity B:
#Override
protected void onResume() {
super.onResume();
// save values here for resume
}
Look the Activity Life Cycle:

Call a Method from another Method Android

I am not sure this workaround is the correct way to achieve my goal of having a prompt text in a spinner. What happens with this application is the spinner navigates to another Activity via an Intent and when the user navigates back to the Main Activity with the spinner they have two ways back. One with a Button and a click event the other by clicking the device BACK button. I am trying to call the code in the click event from the method that manages the device BACK button
I do not know how to call the click event from the device BACK button Method
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(),"Use BACK BUTTON\n\n"+"On the Screen",Toast.LENGTH_LONG).show();
// I want to call goBack(View view) from here
// +++++++++++++++++++++++++++++++++++++++++++
}
public void goBack(View view){
Intent i = new Intent( PageTwo.this, MainActivity.class );
startActivity( i );
}
The reason I use this Intent to navigate BACK to the Main Activity is it reloads the variables in the Spinner
It looks like goBack(View) is most likely from an onClick setup in your layout XML. Since you aren't using the view, just pass null:
#Override public void onBackPressed() {
goBack(null);
}
I don't know if I get you right, if you just want to go back to the activity which started another activity, you can just call finish() method of Activity class:
#Override
public void onBackPressed() {
finish();
}
finish() reference

Categories

Resources