I have successfully used androidannotations #Extra to decode an intent and get the sent message as this snippet demonstrates:
#Extra(MyActivity.MESSAGE)
String intentMessage;
#ViewById(displayMessage)
TextView textView;
#AfterViews
protected void init() {
textView.setText(intentMessage);
}
I'm missing how, if possible, to create the intent in the first place using annotations. e.g replace the following
Intent intent = new Intent(this,DisplayMessageActivity_.class);
intent.putExtra(MESSAGE, s);
startActivity(intent);
with something. Is this possible? (I'm totally new to all this so probably am missing something terribly obvious)
SOLUTION:
DisplayMessageActivity_.intent(this).intentMessage(s).start();
Where intentMessage is the name of the extra field.
Yes, you can use the following:
// Starting the activity
MyListActivity_.intent(context).start();
// Building an intent from the activity
Intent intent = MyListActivity_.intent(context).get();
// You can provide flags
MyListActivity_.intent(context).flags(FLAG_ACTIVITY_CLEAR_TOP).start();
// You can even provide extras defined with #Extra in the activity
MyListActivity_.intent(context).myDateExtra(someDate).start();
// startActivityForResult() equivalent:
MyListActivity_.intent(context).startForResult();
Source: https://github.com/excilys/androidannotations/wiki/HowItWorks
Solution. Finally saw the pattern on how it works. Thanks.
DisplayMessageActivity_.intent(this).intentMessage(s).start();
where intentMessage is the #Extra defined in the activity to be started e.g
#EActivity(R.layout.activity_display)
public class DisplayMessageActivity extends Activity {
public static final String MESSAGE = "net.richardriley.MyFirst.MESSAGE";
#Extra(MESSAGE)
String intentMessage;
#ViewById(displayMessage)
TextView textView;
#AfterViews
protected void init() {
textView.setText(intentMessage);
}
}
I know this is a late answer but I have developed a library that does exactly this. It uses annotations to generate the intent code.
Check it out in here: https://github.com/kostasdrakonakis/android_navigator
Related
I am a novice, just making my first app, so please excuse my poor phrasing and understanding.
I have made a Class MyAlarms() to help manage my alarm and notification code. I require context for a few functions, namely Toast, SharedPreferences, and PendingIntent. GetContext() does not work, nor does getApplicationContext(), so I decided to go with passing the context to MyAlarms(Context context) from the code using it. This works just fine.
But I have now read that I should not be doing this, as to may lead to memory leak issues. Is there another way to get context in a class?
Thanks in advance!
I'm assuming you need the context because you are creating some views? If so, then you need to use .getContext() from whatever layout you are placing that view in.
If you have a static function, you have to pass in the Context from the Activity in the parameters.
For example:
private static void sendMessageToActivity(String msg) {
Intent intent = new Intent("intentKey");
intent.putExtra("key", msg);
LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
}
Change to:
#Override
protected void onCreate(Bundle savedInstanceState) {
...
sendMessageToActivity(this,"test");
}
private static void sendMessageToActivity(Context context, String msg) {
Intent intent = new Intent("intentKey");
intent.putExtra("key", msg);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
I am learning the android room with a view. I've looked through some sample projects and tutorials and there is one thing in this example that I am hung up on and that I do not understand. Here is the code (underneath the code I point out the few lines I'm confused about):
public class NewWordActivity extends AppCompatActivity {
public static final String EXTRA_REPLY = "com.example.android.wordlistsql.REPLY";
private EditText mEditWordView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_word);
mEditWordView = findViewById(R.id.edit_word);
final Button button = findViewById(R.id.button_save);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent replyIntent = new Intent();
if (TextUtils.isEmpty(mEditWordView.getText())) {
setResult(RESULT_CANCELED, replyIntent);
} else {
String word = mEditWordView.getText().toString();
replyIntent.putExtra(EXTRA_REPLY, word);
setResult(RESULT_OK, replyIntent);
}
finish();
}
});
}
}
The parts that I am confused about are the second line, the EXTRA_REPLY, and then you can see it used toward the bottom in reply.Intent.putExtra. What is the EXTRA_REPLY pointing to exactly? How would you find it in your own project?
Here is the source of the sample if you need more context: https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#12
This is just a simple key/value pair. Like a HashMap, or a Dictionary.
replyIntent.putExtra(EXTRA_REPLY, word);
This will set the EXTRA_REPLY to word within your Intent so you can read it in whatever Activity is handling your result.
You can also do it with a Bundle, for example when you launch a new Activity.
The key, com.example.android.wordlistsql.REPLY, does not really matter, just make sure that everyone is using the same key.
When you make an intent you can add extra data to it in key-value form. In this example they use the string constant EXTRA_REPLY as the key, and the variable word as the value.
The recipient of the intent would access the data by using intent.getStringExtra(NewWordActivity.EXTRA_REPLY).
It's good practice to define the keys for your intents as constants so that you're less likely to make mistakes when referencing it in other classes.
I am learning Android (I'm VERY newbie at the moment).
I was looking and reading another posts but I have not find this exactly (or not simple).
I want to pass a arraylist from an activity to a intent service, I think this is the simplest manner to do it; however I get NullPointer Exception.
public class MainActivity extends Activity {
private static final String TAG = "Main";
ArrayList<String> lista_actual = new ArrayList<String>();
...
public void onClick (View v) {
Intent msgIntent = new Intent(MainActivity.this, MiIntentService.class);
lista_actual.add("probasndo cad");
lista_actual.add("dfafsadf");
lista_actual.add("dfasf");
msgIntent.putStringArrayListExtra("lista", lista_actual);
msgIntent.putExtra("iteraciones", 10);
startService(msgIntent);
//copiar();
}});
Then where I try get the array:
protected void onHandleIntent(Intent intent)
{
ArrayList<String> lista_archivos = intent.getStringArrayListExtra("lista_actual");
Log.d ("Intent", Integer.toString(lista_archivos.size()));
.
.
.
Thanks.
While you are fetching your array list in intent service ur calling wrong key, it should be :
ArrayList<String> lista_archivos = intent.getStringArrayListExtra("lista");
Log.d ("Intent", Integer.toString(lista_archivos.size()));
Replace lista_actual with lista.
You need to serialize the arraylist in order to pass between activities. Further help will be found here. Hope that helps
I need to compute a paramater value in Activity and send back the result.
My code will look like this:
public String getMyValue(){
Intent intent = new Intent(context, MyWebView.class);
context.startActivity(intent);
return myValue;
}
Below is my MyWebView class
MyWebView extends Activity{
protected void onCreate(){
myValue = a+b;
}
}
So, now if I call below code, I am getting null 'null' every time.
String a = getValue();
My question is how I have to return 'myValue' after it get updated in the activity.
Please let me know if there is other way to do this.
My question is how I have to return 'myValue' after it get updated in
the activity.
For getting result from and back from Activity, you need to use startActivityForResult(). There are plenty of examples available on web so not including here.
In order to simplify some code, I'm wondering if it is possible to parameterize code that launches activities in an android application, so that instead of having 5
public void showSettings(View view) {
Intent SettingsActivity = new Intent(MainActivity.this, Settings.class);
startActivity(SettingsActivity);
I can do something like the following
public void showActivity(View view, String ActivityName) {
Intent ActivityName = new Intent(MainActivity.this, ActivityName.class);
startActivity(ActivityName);
Then, for each button in the UI, I simply apply the following to the "onclick" event
showActivity(Settings);
or
showActivity(domains);
This would save about 40-50 lines of code in my app. Obviously I know the above code is incorrect, but I'm not sure if it's possible to do what I'm trying to accomplish.
How about something like:
public <T> void showActivity(View view, Class<T> activity) {
Intent activityName = new Intent(MainActivity.this, activity);
startActivity(activityName);
}
You can invoke it with
showActivity(Settings.class);
I'd recommend use ACTIONs (String) instead of specifying exactly context and class. This way you even can share activities among applications, and if you decide to switch to different activity class, you can edit only android manifest, instead of editing all java source code calls this activity.