Java/Android How to call an activity into a method? - java

How do i call on a seperate activity within a method:
For example:
private void startApp() {
Patient_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// I want this Button to go to an Detailed_ModeActivity
// This is how i Am doing it right now, but it comes out with an
// error
Intent b = new Intent(this, Detailed_ModeActivity.class);
startActivity(b);
}
});
}
Any Help would be appreciated.
The Button was declared in the onCreate method

Like this:
Intent b = new Intent(v.getContext(), Detailed_ModeActivity.class);
startActivity(b);

The this refers to an View.OnClickListener object which doesn't have startActivity() method and cannot be passed to an Intent. You need to call startActivity() on a Context (e.g. an Activity). Let's say your code is in the MainActivity class. Like this:
Intent b = new Intent(MainActivity.this, Detailed_ModeActivity.class);
MainActivity.this.startActivity(b);

First up, make sure Detailed_ModeActivity extends Activity.
Secondly you need to add the activity class to the manifest.xml file if you haven't already.

Related

Is there a way to create a class to share a transition intent? [building my first android app]

What I am trying to achieve here is create a java class that will allow me to call the method inside the class for an activity transitioning.
For instance, I have 3 activities: Main.xml, A activity.xml, B activity.xml. I want to create a class that I can call from main's java class to transition to A and then from A to B.
I thought maybe it would be somewhere along that lines of:
[Transitioning activity class]
//Behind the scenes for activity transitioning, I don't know how to write this part or if calling it is correct
Intent intent = new Intent(?, ?); startActivity(intent); overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
[Main]
//calling intent from activity transitioning class
intent(this, A_activity.class);
[A Activity]
//calling intent from activity transitioning class
intent(this, B_activity.class);
Also I want to apologize, this is indeed my first rodeo, I am self-taught and have no professional experience in programming or developing. Thank you in advance!
So far what I am currently doing is creating the transitions in each class:
[A activity]
public void onClick(View v){
switch(v.getId()){
case R.id.commBtn:
inqList.clear();
inqAdd.addItem("Commercial");
System.out.println(inqList);
onTransition();
break;
case R.id.resBtn:
inqList.clear();
inqAdd.addItem("Residential");
System.out.println(inqList);
onTransition();
break;
default:
break;
}
}
//Method for transition after clicked
public void onTransition(){
Intent intent = new Intent(this, b_scene.class);
startActivity(intent);
overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
}
**[Edit]**
///I did this and it works? not sure if its best practice but it worked.
[Transition Class]
public class Transition {
public static void onTrans(Activity activity, Class name) {
Intent intent = new Intent(activity, name);
activity.startActivity(intent);
activity.overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
}
}
[A Activity]
///Just put this in A activity
public void onClick(View v){
...
onTrans(this, B_Activity.class)
}
Sure you can but since that transition class is not an Activity instance (which has the startActivity() method), you need to pass the Activity instance as well, e.g.:
public static void navigateTo(Activity source, Activity destination) {
Intent intent = new Intent(activity, destination.class);
source.startActivity(intent);
source.overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
}
Another common option is to use an abstract BaseActivity that has this method onTransition() and extend your activities from this one (you can also use composition instead of inheritance for a better practice).

How do you start a new window activity in Android Studio?

I tried several things, such as:
Intent i = new Intent(this, myActivity.class);
startActivity(i)
Where it tells me it cannot resolve symbol 'myActivity'
I tried Context.startActivity and extended the class by Context, at that point it just wants me to implement every single method of Context into my class.
How can I just simply make a new activity visible to the user after an if condition runs into true?
Intent i = new Intent(CurrentActivity.this, myActivity.class);
startActivity(i)
Here myActivity is the name of your second activity (Which you want to open from this intent).
CurrentActivity is the name of your current activity
First you need to understand how to define a class name(Should always start from a capital letter). Then replace the 'myActivity.class' with your new class name.
Make sure that your activity is defined in the AndroidManifest.xml
<activity android:name=".YourActivityName" />
if this doesn't fix the problem then try:
Intent myIntent = new Intent(view.getContext(), MyClass.class);
if view is not found, then implement your class with
public class YourClassName extends AppCompatActivity implements View.OnClickListener{#Override
public void onClick(View v) {
}}
or if you are in a fragment then:
Intent myIntent = new Intent(MyFragment.this.getActivity(), MyClass.class);

Android skip Activity and put it last

I am trying to make a skip button and it have to do what i draw.
i honestly don't know how can i make this happen.
The code actually have to skip the current activity and put it as last.
if the activity's question was answered, there is no skip.here i draw
I have tried a few things but it's beyond my powers.
Intent intent=new Intent(this, c1_2.class);
startActivity(intent);
finish();
you can use arrayList or Queue for keep your activityes, then use it to start Activity.
try this.
final ArrayList<Class> activities = new ArrayList<>();
// add your activity---------------
activities.add(c1_1.class);
activities.add(c1_2.class);
activities.add(c1_3.class);
//----------------------------
butonSkip=findViewById(R.id.skip);
butonSkip.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Class firstActivity = activities.get(0);
startActivity(new Intent(c1_1.this,activities.get(0)));
activities.remove(0);
activities.add(firstActivity);
}
});
//-----------can change activities data
// activities.set(1, c1_3.class);

How to start a new activity and start a method in that activity

So I'm working with Java in android studio and I want to start a new class from a different class (I'm in ListenerServiceFromWear and want to start MainActivity) and once Mainactivity is started I want to start a method (startEmergencyMode();) in Mainactivity.
How do I do this from ListenerServiceFromWear?
Start MainActivity with an intent and in the extra of the intent put some flag that will tell MainActivity to call startMergencyMode()
Intent intent = new Intent(this, Mainactivity.class);
intent.putExtra("isEmergency", true);
startActivity(intent);
And then in Mainactivity actually call startEmergencyMode()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
Intent intent = getIntent();
boolean isEmergency = intent.getBooleanExtra("isEmergency", false);
if(isEmergency){
startEmergencyMode();
}
}
I don't quite understand what you mean by "start"
In java, you either:
Declare a static field or method
Create an instance of an object and use its public fields and methods.
If you wish to just have one 'instance' of MainActivity, use a static method:
public static void startEmergencyMode() {
// Code here
}
Which you can call anywhere using MainActivity.startEmergencyMode().
Keep in mind that this static method can only access static fields and other static methods.
If you wish to create an instance of MainActivity, simply create one and call the method:
public void startEmergencyMode() {
// Code here
}
// Somewhere else
MainActivity activity = new MainActivity();
activity.startEmergencyMode();
If you don't understand the difference between a static and non static method or field, refer the answer on this thread: What does 'public static void' mean in Java?

Android Intent in public function

Hi i am trying to make a public function in order to change activities/changes in my android application.
In a external class (Utl.java) I have this code.
public class Utl extends Activity{
public void onBtnClicked(View v) {
Intent i;
Integer data=0;
switch(v.getId()){
case R.id.btnStart:
i=new Intent(getApplicationContext(), LevelChoose.class);
startActivity(i);
break;
case R.id.btnEasy:
i=new Intent(getApplicationContext(), PlayGame.class);
startActivity(i);
break;
case R.id.btnMedium:
i=new Intent(getApplicationContext(), PlayGame.class);
data=48;
i.putExtra("extra", data);
startActivity(i);
break;
case R.id.btnHard:
i=new Intent(getApplicationContext(), PlayGame.class);
data=96;
i.putExtra("extra", data);
startActivity(i);
break;
}
}
}
and in my xml's files i use the on click function of the buttons...
<Button
android:id="#+id/btnStart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="42dp"
android:minHeight="110dp"
android:text="Start Game"
android:onClick="cls2.onBtnClicked(this)" />
I get that the function can't be found...
I wish to dynamically change screens.... Thank you for your time!!
You can use the constructor where you can assign the current context. Try this example
public void sampleMethod(final Activity cont)
{
Intent intent = new Intent(cont, XYZ.class);
cont.finish();
cont.startActivity(intent);
}
also move this method to a util class and call it from activity and pass the activity reference as follows
class ActivityB extends Activity
{
Util.sampleMethod(this);
}
You have multiple issues in your code. I'll try to list them here:
You cannot pass a method of any class to android:onClick.
You'll have to set the onClickListener for your Buttons in code, using
yourButton.setOnClickListener(new Utl());
Don't make Utl a subclass of Activity. Use Activity only for different "screens". You'll want to use Object instead here. If you need a Context in Utl, add a class member and take a Context in the constructor, like this:
private Context mContext;
public Utls (Context c){
mContext = c;
}
Now, instantiate Utls like this:
yourButton.setOnClickListener(new Utl(this));
Just change this line
android:onClick="cls2.onBtnClicked(this)"
to
android:onClick="onBtnClicked"
View will be passed to function automatically.
Happy coding..:)

Categories