Android:从片段调用活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12637693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android : Calling Activity from Fragment
提问by Dharma Cool
I am using fragments inside an activity. I am using MediaRecorder to for audio recording. I have two part of an activity. 1st itself the Activity which will list the recorded file. On it's right side the AudioRecording Activity is called when one select to record for a new file. When the any of the listed file is selected i am using AudioPlayer as to play the recorded file. I am here able to convert the Activity into fragment but when i press on Stop it is terminating the application.
我在活动中使用片段。我正在使用 MediaRecorder 进行录音。我有一个活动的两个部分。第一个本身将列出录制文件的活动。在它的右侧,当一个选择要为新文件进行记录时调用查找活动。When the any of the listed file is selected i am using AudioPlayer as to play the recorded file. 我在这里能够将活动转换为片段,但是当我按下停止时,它正在终止应用程序。
Please anyone can answer. My audiorecorder is working fine when i use it as simple activity. Any solution like if i can call that activity in that fragment or something like that.? Please help me if anyone knows.
请任何人都可以回答。当我将它用作简单活动时,我的录音机工作正常。任何解决方案,例如我是否可以在该片段中调用该活动或类似的东西。?如果有人知道,请帮助我。
回答by Chinmoy Debnath
Get the parent activity using get activity then do as usual.
使用 get 活动获取父活动,然后照常执行。
Intent myIntent = new Intent(getActivity(), BookmarkActivity.class);
getActivity().startActivity(myIntent);
回答by Ishara Amarasekera
Here is another alternative method. This worked for me.
这是另一种替代方法。这对我有用。
public class **YourFragmentClass** extends Fragment {
Context context; //Declare the variable context
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Pass your layout xml to the inflater and assign it to rootView.
View rootView = inflater.inflate(R.layout.**yourfragmentxml**, container, false);
context = rootView.getContext(); // Assign your rootView to context
Button **yourButton** = (Button) rootView.findViewById(R.id.**your_button_id**);
**yourButton**.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Pass the context and the Activity class you need to open from the Fragment Class, to the Intent
Intent intent = new Intent(context, **YourActivityClass**.class);
startActivity(intent);
}
});
return rootView;
}
}
回答by SubbaReddy PolamReddy
To call another activity
from fragment
use this:
activity
从fragment
使用这个调用另一个:
Intent i = new Intent(getActivity(), Activity.class);
startActivity(i);
回答by Divyesh V.Murani
In Fragment Class
在片段类中
getActivity().startActivity(new Intent(gwtActivity(),MainActivity.class));
getActivity().finish();
回答by Krishna Meena
Best way of calling Activity from Fragment class is that make interface in Fragment and add onItemClick()
method in that interface. Now implement it to your first activity and call second activity from there.
从 Fragment 类调用 Activity 的最佳方法是在 Fragment 中创建接口并onItemClick()
在该接口中添加方法。现在将它实施到您的第一个活动并从那里调用第二个活动。
回答by Ibrahim Al-Tatary
You can simply call
你可以简单地打电话
startActivity(new Intent(getActivity(),TheNextActivity.class));
回答by Ashok Reddy M
Your fragment should have a parent
你的片段应该有一个父级
Intent intent = new Intent(getActivity(), SecondActivity.class);
getActivity().startActivity(intent);