How to put extra data in a dialog? - android

I want to register a callback for dialog.setOnCancelListener ( OnCancelListener),
but this callback will be registered many times for other dialogs,So I should get some unique date from passed dialog with different extra date to know which one is useful or unuseful.

Use an interface, put this code in your Dialog Fragmnet:
public static interface MyInterface {
public void cance(String someInfo);
}
private MyInterface mListener;
#Override
public void onAttach(Activity activity) {
mListener = (MyInterface) activity;
super.onAttach(activity);
}
#Override
public void onDetach() {
mListener = null;
super.onDetach();
}
Then, in the onCancel method:
#Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
super.onCancel(dialog);
//This line passes the String to the implementing class
mListener.onChoose(choice);
}
Back in your class:
public class MainActivity extends Activity implements MyInterface {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
public void onChoose(String myExtraData) {
//Do stuff here
}

Related

How to write an app with repetitive parts?

I want to write an app with about 20 different Activity. Inside each activity there is some parts that are similar in some other activities. I divided each page to smart parts and write a separate layout for them. Then in activities I use include tag for adding that part to activity.
I don't know what is the true way to implement this app.
Thanks for any suggestion.
I have made sample BaseActivity. Make abstract method you want to use and just override to your class. You can use all Base Activity public method in your class. abstract method will override to your class and simple Public method will be optional if you want to use
public abstract class BaseMainActivity extends AppCompatActivity implements View.OnClickListener{
private static ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initMethod();
setContentView(getLayout());
initUI();
initListeners();
}
public abstract void initMethod();
public abstract int getLayout();
public abstract void initUI();
public abstract void initListeners();
#Override
public void onClick(View view) {
}
public String getEditString(CustomEdittext edittext){
return edittext.getText().toString().trim();
}
public void getEditError(CustomEdittext edittext, String message){
edittext.setError(message);
}
public static void showProgress(AppCompatActivity activity){
try{
progressDialog = new ProgressDialog(activity);
progressDialog.setTitle("Please Wait");
progressDialog.show();
}catch (Exception ex){
ex.printStackTrace();
}
}
public static void hideProgress(){
try{
if(progressDialog!=null)
progressDialog.dismiss();
}catch (Exception ex){
ex.printStackTrace();
}
}
public void showToast(String message,Context context)
{
Toast.makeText(context,message,Toast.LENGTH_LONG).show();
}
}
Hi this is sample that we can extend BaseActivity to our class.
public class HomeActivity extends BaseMainActivity {
private Toolbar toolbar = null;
private Picasso picasso;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void initMethod() {
}
#Override
public int getLayout() {
return R.layout.activity_main;
}
#Override
public void initUI() {
initToolbar();
initDrawerLayout();
initNavigationView();
}
}
You can create base activity for similar methods and extend it in other activities.
And about layouts you can create similar layouts at first then include them to other layouts.
And for good structure i think use MVP.

Why do I get an error of cycling inheritance involving MyFragment?

I am trying to send data from activity to fragment and vice versa using interfaces but getting an error of cycling inheritance involving MyFragment.
Implementing interface created in MyFragment:
public class MyActivity implements OnSendFromMyFragListener {
OnSendFromMyActivityListener mCallback;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCallback.sendFromMyActivity(2);
}
#Override
public void sendFromMyFrag (int a) {
//do something
}
public interface OnSendFromMyActivityListener {
public void sendFromMyActivity(int b);
}
}
Implementing interface created in MyActivity:
public class MyFragment extends Fragment implements OnSendFromMyActivityListener {
OnSendFromMyFragListener mCallback;
public interface OnSendFromMyFragListener {
void sendFromMyFrag(int a);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mCallback = (OnSendFromMyFragListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString());
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my, container, false);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCallback.sendFromMyFrag(1);
}
}
});
return view;
}
#Override
public void sendFromMyActivity(int b) {
//do something
}
}
Well the reason you are getting this error is because your Activity depends on your Fragment and Fragment depends on your Activity. Don't Agree?
Let me show you. Imagine you are a compiler in your work you stumbled across:
class A implements B.A {
interface B {
void foo1();
}
#Override
public void foo2()
{
// do something;
}
}
Now you know that class A depends on (implements) B.A, so before you go further into class A you move on to class B:
class B implements A.B {
interface A {
void foo2();
}
#Override
public void foo1()
{
// Do Something;
}
}
Now you see that class B depends on (implements) class A specifically class A.B! What do you (compiler) do? Go back to class A? But that depends on class B. So you see this becomes an unending cycle thus causing a cyclic dependency where both your class' definitions depend on each other.
As an alternative you could either create a member event listener or an anonymous one. Or if you don't like either of those options, you could also create a separate java interface class file for any one of the two interfaces.
Maybe you have to do this for Activity
public class MyActivity extends AppCompatActivity implements MyFragment.OnSendFromMyFragListener {
public interface OnSendFromMyActivityListener {
void sendFromMyActivity(int b);
}
OnSendFromMyActivityListener mCallback;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyFragment myFragment = MyFragment.newInstance();
//Do the transaction.....
//And after this
mCallback.sendFromMyActivity(0);
}
public void setOnSendFromMyActivityListener(OnSendFromMyActivityListener mCallback){
this.mCallback = mCallback;
}
#Override
public void sendFromMyFrag(int a) {
//do something
}}
And this for Fragment
public class MyFragment extends Fragment implements OnSendFromMyActivityListener {
OnSendFromMyFragListener mCallback;
public interface OnSendFromMyFragListener {
void sendFromMyFrag(int a);
}
public static MyFragment newInstance() {
Bundle args = new Bundle();
MyFragment fragment = new MyFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mCallback = (OnSendFromMyFragListener) getActivity();
((MyActivity) getActivity()).setOnSendFromMyActivityListener(this);
} catch (ClassCastException e) {
throw new ClassCastException(context.toString());
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_place_suggest, container, false);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCallback.sendFromMyFrag(1);
}
});
return view;
}
#Override
public void sendFromMyActivity(int b) {
//do something
}
if you want you can put setters of the interface also to the Fragment for better abstraction.
Something you have to watch out is to look for nulls interfaces
Thanks

How to have an Activity notify a Fragment that the back button has been pressed

I have been researching this for a few days and have yet to find a working solution. There is lots of information available but because of my inexperience with Android I can't get any of the suggestions to work.
I have an Activity with a stack of 3 Fragments on top of it all of which are presented using FragmentManager Transactions and added to the backstack. While the third Fragment is active, I need to intercept the onBackPressed() method and perform some extra stuff before the Fragment is destroyed.
I have tried using Callbacks and Interfaces to capture onBackPressed() at the Activity and send it to the 3rd Fragment with no luck.
What is the proper way to have a Fragment deep in the stack watch for the Activity's onBackPressed() method.
Let me know if this is not clear.
Thanks for the help.
Not compiled and tested, but this lays out the basic approach:
public interface BackButonListener {
boolean OnBackButtonPressed();
}
public interface BackButtonWatchable {
void addBackButtonListener(BackButtonListener listener);
void removeBackButtonListener(BackButtonListener listener);
}
public class MyActivity extends Activity implements BackButtonWatchable {
...
private static ArrayList<BackButtonListener> backButtonListeners
= new ArrayList<BackButtonListener>();
#Override
public void addBackButtonListener(BackButtonListener listener) {
backButtonListeners.add(listener);
}
#Override
public void removeBackButtonListener(BackButtonListener listener) {
backButtonListeners.remove(listener);
}
...
#Override
public void onBackButtonPressed()
{
boolean supressBackButton = false;
for (BackButtonListener listener: backButtonListeners)
{
if (!listener.OnBackButtonPressed()) {
suppressBackButton = true;
}
}
if (!suppressBackButton) {
super.onBackButtonPressed();
}
}
}
public class MyFragment extends Fragment implements BackButtonListerer {
#Override
public void onResume()
{
((BackButtonWatchable)getActivity()).addBackButtonListener(this);
}
#Override
public void onPause() {
((BackButtonWatchable)getActivity()).removeBackButtonListener(this);
}
}
Crete interface
public interface OnBackPressedListener {
void onBackPressed();
}
and create field in activity
private OnBackPressedListener mListener;
and your onBackPressed() should look like
if (mListener != null) {
mListener.onBackPressed();
} else { /* do your acitivty usual stuff */ }
When fragment is created you register this fragment as mListener in your activity and don't forger to set it to null in onDestroy.
This is the post that answered my question. For a Android newbie, this told me where everything needed to go.
https://stackoverflow.com/a/30865486/2640458
The Fragment that needed to see the onBackPress() method from it's activity:
public class RatingFragment extends Fragment implements ContentActivity.OnBackPressedListener {
#Override
public void doBack() {
getFragmentManager().popBackStack();
}
The very important subscription to the listener in the above Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_rating, container, false);
((ContentActivity)getActivity()).setOnBackPressedListener(this);
}
The Activity that needs to send the onBackPress() method to the above Fragment:
public class ContentActivity extends Activity {
protected OnBackPressedListener onBackPressedListener;
public interface OnBackPressedListener {
void doBack();
}
public void setOnBackPressedListener(OnBackPressedListener onBackPressedListener) {
this.onBackPressedListener = onBackPressedListener;
}
#Override
public void onBackPressed() {
if (onBackPressedListener != null)
onBackPressedListener.doBack();
else
super.onBackPressed();
}
#Override
protected void onDestroy() {
onBackPressedListener = null;
super.onDestroy();
}
}

Android AsyncTask re-runs when activity is opened

In my App the user has to login.
They open the app on the login page
They enter email/password and hit login
A LoadingScreenActivity is opened that has a swirly circle and is running an AsyncTask that goes to my database and retrieves all the users info
After the AsyncTask is completed it starts an intent to launch MainPageActivity.
There are two problems with this at the moment:
If the user logs in and then goes to the home screen while the app loads the MainPageActivity will open as soon as it is ready (on top of the existing home page) even though the app has been minimised
If the user logs in and then goes to the home screen while the app loads and then returns to the loading screen the AsyncTask will complete twice over
For problem 1. At the moment my onPostExecute() method in LoadingScreenActivity looks like this:
#Override
public void onPostExecute() {
//open the main page
Intent mainPage = new Intent(getApplicationContext(), MainPageActivity.class);
mainPage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mainPage.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
startActivity(mainPage);
}
Is there a way I could detect in this method if the main page activity should be opened yet?
For problem 2. I've hit a complete road block on this, is there a way to detect if the activity has simply been re opened rather than started for the first time? I'd really appreciate any tips on this, I'm quite new to android so I'm not even convinced an Async task is the way to go with this.
Thanks for your time
LoadingScreenActivity.java
public class LoadingScreenActivity extends Activity implements TaskFragment.TaskCallbacks {
private static final String TAG_TASK_FRAGMENT = "task_fragment";
private TaskFragment mTaskFragment;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
FragmentManager fm = getFragmentManager();
mTaskFragment = (TaskFragment) fm.findFragmentByTag(TAG_TASK_FRAGMENT);
// If the Fragment is non-null, then it is currently being
// retained across a configuration change.
if (mTaskFragment == null) {
mTaskFragment = new TaskFragment();
fm.beginTransaction().add(mTaskFragment, TAG_TASK_FRAGMENT).commit();
}
setContentView(R.layout.loading_screen);
ActionBar actionBar = getActionBar();
actionBar.hide();
TextView title = (TextView) findViewById(R.id.loading_title);
TextView progress = (TextView) findViewById(R.id.loading_progress);
title.setText(R.string.app_name);
progress.setText("Loading your info");
}
#Override
public Context onPreExecute() {
return getApplicationContext();
}
#Override
public void onProgressUpdate(int percent) {
}
#Override
public void onCancelled() {
Intent login = new Intent(getApplicationContext(), LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
finish();
}
#Override
public void onPostExecute() {
//open the main page
Intent mainPage = new Intent(getApplicationContext(), MainPageActivity.class);
mainPage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mainPage.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
startActivity(mainPage);
}
}
and TaskFragment.java
public class TaskFragment extends Fragment {
static interface TaskCallbacks {
Context onPreExecute();
void onProgressUpdate(int percent);
void onCancelled();
void onPostExecute();
}
private TaskCallbacks mCallbacks;
private DummyTask mTask;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mCallbacks = (TaskCallbacks) activity;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Retain this fragment across configuration changes.
setRetainInstance(true);
// Create and execute the background task.
mTask = new DummyTask();
mTask.execute();
}
#Override
public void onDetach() {
super.onDetach();
mCallbacks = null;
}
private class DummyTask extends AsyncTask<Void, Integer, Void> {
Context context;
boolean running = true;
#Override
protected void onPreExecute() {
if (mCallbacks != null) {
context = mCallbacks.onPreExecute();
}
}
#Override
protected Void doInBackground(Void... ignore) {
//Get the current thread's token
synchronized (this)
{
if(running){
DatabaseHandler dbHandler = new DatabaseHandler(context);
dbHandler.populateSQLiteDatabase();
}
}
return null;
}
#Override
protected void onProgressUpdate(Integer... percent) {
if (mCallbacks != null) {
mCallbacks.onProgressUpdate(percent[0]);
}
}
#Override
protected void onCancelled() {
if (mCallbacks != null) {
mCallbacks.onCancelled();
}
}
#Override
protected void onPostExecute(Void ignore) {
if (mCallbacks != null) {
mCallbacks.onPostExecute();
}
}
}
}
in your activity in the manifest just add android:configChanges="keyboardHidden|orientation|screenSize"
and in the activity implement this
#Override
public void onConfigurationChanged(Configuration newConfig) {
};
I'm afraid, i don't really understand your first problem.
About the second one, there are a couple of ways depending on your minimum API level. Starting from API 14 you may register ActivityLifecycle Callbacks inside an Android Application. To do this, i would recommend:
Inherit Android application with a custom one
Replace the Android application in your manifest
inside your custom application register itself as activity lifecycle listener
inside the abstract methods you get the instance of the currently applying activity (may safe object.name() in a String)
depending on your handling you may safe a boolean value or whatever to identify the behaviour
methods inside your custom application are accessible by casting (MyCustomApplication)getApplication()
Heres a snippet:
package com.example.preferencestest;
import android.app.Activity;
import android.app.Application;
import android.app.Application.ActivityLifecycleCallbacks;
import android.os.Bundle;
public class MyCustomApplication extends Application implements ActivityLifecycleCallbacks {
private String storedActivity;
private Boolean doOrNot = false;
public MyCustomApplication() {}
public Boolean getDoOrNot() { return doOrNot;}
public void setDoOrNot(Boolean doOrNot) { this.doOrNot = doOrNot; }
#Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(this);
}
// these two are the most important ones since they will be fired everytime
#Override
public void onActivityResumed(Activity activity) {
if (activity.getClass().getName().equals(storedActivity)) {
doOrNot = true;
}
}
#Override
public void onActivityPaused(Activity activity) {
storedActivity = activity.getClass().getName();
}
#Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) { }
#Override
public void onActivityStarted(Activity activity) { }
#Override
public void onActivityStopped(Activity activity) { }
#Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) { }
#Override
public void onActivityDestroyed(Activity activity) { }
}
inside your Manifest you MUST declare this new Applicationclass like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.preferencestest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<application
android:name="com.example.preferencestest.MyCustomApplication"
{...}
Then inside your Activity you may do this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Boolean whatToDo = ((MyCustomApplication)getApplication()).getDoOrNot();
}
Using onConfigurationChanged has a couple of disadvantages. There are a couple of actions (placing device in dock, turning display and so on) which restart the Activity. You should rather save the current state of the Activity with
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
}

how to send data in both directions using an interface between Activity and Fragment

i made an interface in the fragment class kind of like a listener to send a string from the Fragment to the Activity. what is the best way to also send strings in the opposite direction? from activity to fragment? so there is now an interface in the fragment, so do i have to make another interface in the Activity to send strings in the other direction, or is there a better way?
the fragment class:
public class FragmentHeadless extends Activity implements FragmentHeadlessFragment.OnTimeRequestedListener {
FragmentTransaction transaction;
Button button;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_headless);
button = (Button) findViewById(R.id.button1);
textView = (TextView) findViewById(R.id.textView1);
FragmentManager fragmentManager = getFragmentManager();
transaction = fragmentManager.beginTransaction();
transaction.add(new FragmentHeadlessFragment(), "processorFragment");
transaction.commit();
}
#Override
public void passString(String stringFromFragment) {
textView.setText(stringFromFragment);
Toast.makeText(this, "String passed from fragment " + stringFromFragment, Toast.LENGTH_SHORT).show();
}
}
the activity class
public class FragmentHeadlessFragment extends Fragment {
private OnTimeRequestedListener listener;
String tempString = "";
Activity activity;
Handler handler;
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
handler = new Handler();
new Thread(){
public void run() {
SystemClock.sleep(3000);
handler.post(new Runnable() {
#Override
public void run() {
// pass string to activity
passData("test string from fragment with 3 second delay");
}
});
} // end run
}.start();
} // end onCreate
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof OnTimeRequestedListener) {
listener = (OnTimeRequestedListener) activity;
} else {
throw new ClassCastException(activity.toString()
+ " must implemenet OnTimeRequestedListener");
}
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
activity = getActivity();
setRetainInstance(true);
}
public interface OnTimeRequestedListener {
public void passString(String timeNumberString);
}
public void passData(String data) {
listener.passString(data);
}
}
since you have an instance to the fragment, you can just call the function you need via the activitiy:
instead of :
transaction.add(new FragmentHeadlessFragment(), "processorFragment");
use:
private FragmentHeadlessFragment mFragment;
...
mFragment=new FragmentHeadlessFragment();
transaction.add(mFragment, "processorFragment");
and when you wish, call:
mFragment.foo(myInt,myString,...) ;
btw, why do you have a field for the transaction?

Categories

Resources