Android own Activity start function - android

i've tried to start an activity with my own function, but it doesn't work (think its a logic fault ;) )
Please have a look at:
Intent adder = new Add().execute(this, Add.class, "Test:","Add the test");
startActivity(adder);
This is the Activity which i want to start:
public class Add extends Activity {
public Intent execute(Context context, Class<?> cls, String addText, String buttonText) {
TextView addStr = (TextView) findViewById(R.id.addtext);
addStr.setText(addText);
Button addBtn = (Button) findViewById(R.id.addbtn);
addBtn.setText(buttonText);
return new Intent(context, cls);
}
}
What's wrong?

You should take a look at this Android developer guide, which talks about Activity and Intent. I think you will see that they are used in a completely different way than you are trying to use them.

Related

My app doesn't open the correct activity - why so?

Good afternoon
I'm developing an Android App and i'm having some trouble with it.
I have many buttons it the app that are supposed to open activities. I don't want to have a lot of different onClick() methods so i'm trying to make one generic that can retrieve the correct activity to open depending on which button was clicked.
I came up with this code that works correctly:
public void displayActivity(View view) {
switch(view.getId()) {
case R.id.secondActitityButton:
myClass = SecondActivity.class;
break;
default:
throw new RuntimeException("Unknown Button");
}
Intent myIntent = new Intent (this, myClass);
startActivity (myIntent);
}
But this still means that i need to have a lot of switch cases (one for each button...). So i tried another aproach using the "Content Description" of the button to store the class name. This is the code:
public void displayActivity(View view) {
Class myClass;
String className = (view.getContentDescription().toString().trim());
try {
myClass = Class.forName(className);
}
catch (ClassNotFoundException e) {
myClass = MainActivity.class;
}
Intent myIntent = new Intent (this, myClass);
startActivity (myIntent);
}
The Content Description of the button was defined to "SecondActivity.class".
But now when i click the button it keeps sending me to the MainActivity.
I researched but just couldn't find the answer for this problem...
Can anybody help please?
Thanks in advance
Class#forName() expects a fully qualified name of the desired class. So class name should be something like the.complete.packagename.YourActivity.
Please note that's not the intended usage of content description, see Make apps more accessible. You should consider another approach.

How to share data once an activity has started from another function?

I would like know how to send a command to an activity which has started from inside another function. More precisely I want to send a pause intent to com.google.vr:sdk... the view is started like this:
class VRPlayer {
private void playVideo(int sourceType, Context context) {
Intent intent = new Intent(context, VrVideoActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
private void pauseVideo(Context context, JSONArray args) {
VrVideoActivity.class.toggleVideoPlay(); <------------- NOT WORKING ¯\_(-_- )_/‾
}
}
once the activity has started there should be a way to execute other function, ex: toggleVideoPlay() which are inside the VrVideoAcitivity from the but I can't get the right way to do it... If you want to try it you can find the Android project here: https://github.com/StarStep/android-help-vr
VrVideoActivity vrVideoActivity = new VrVideoActivity();
vrVideoActivity.toggleVideoPlay();
a simple way -> (new VrVideoActivity).toggleVideoPlay();
another way -> set the toggleVideoPlay(); to static as following
public static void toggleVideoPlay()
{
//Your Code
}
and call it VrVideoActivity.toggleVideoPlay();

Android : Can't put a string through an intent from a fragment to an activity

I'm new in android development and I have an issue with simple string in an intent.
I create an Intent in a fragment to launch a new activity. I have no error but I can't put a string through the intent.
Here is my code :
public class AboutFragment extends Fragment {
private static Context mContext;
I have this in the onCreateView method :
mContext = this.getContext();
This method in the same class
public static void openModalPolicy() {
Intent intent = new Intent(mContext, ModalActivity.class);
intent.putExtra("section", "policy");
mContext.startActivity(intent);
}
And in my new activity :
String section = this.getIntent().getStringExtra("section");
So the problem is that section is never equal to "policy"
If someone can help me :) Have a good day !
Just to be sure, you are using section.equals("policy") for your equality check.
Not section == "policy". Right?
Check out this post for more information: How do I compare strings in Java?

No Activity found to handle Intent { act=android.intent.action.CALL

When I run the app there is an Fatal exception error
android.content.ActivityNotFoundException: No Activity found to handle Intent
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get butten
Button bt= (Button) findViewById(R.id.bt);
// set a monitor
bt.setOnClickListener(new MyListener());
}
class MyListener implements View.OnClickListener{
public void onClick(View v) {
EditText et = (EditText) findViewById(R.id.et);
String number = et.getText().toString();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel"+number));
startActivity(intent);
}
}
}
Very simple, your device does not have an app that handles phone calls. It is probably a tablet. When coding, you have to code for such errors, by using try...catch.
You have to specify the Intent by using a context & class name.
Since you haven't provide your Manifest file the easiest way to avoid the error is changing the code as follows
Intent intent = new Intent(this,MainActivity.class);
More details from android Documentation
android.content.Intent public Intent(android.content.Context packageContext,
java.lang.Class<?> cls)
Create an intent for a specific component. All other fields (action, data, type, class) are
null, though they can be modified later with explicit calls. This
provides a convenient way to create an intent that is intended to
execute a hard-coded class name, rather than relying on the system to
find an appropriate class for you; see setComponent for more
information on the repercussions of this.
Parameters: packageContext - A Context of the application package
implementing this class. cls - The component class that is to be
used for the intent.

Using androidannotations to fire an intent

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

Categories

Resources